Results 1 to 3 of 3
- 11-10-2012, 10:49 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Program not waiting for user input
I have used some user input in this program before, however, this time it isn't working. Instead of waiting for the user to input data, the program just continues executing, this means that customerName never gets a value and appears blank in the next line.
Here is the snippet of code:
Java Code:Scanner userInput = new Scanner(System.in); int userSelection = userInput.nextInt(); if (userSelection == 1){ if(seatsAvailibleAtoC == 0){ System.out.println("I'm sorry, there are no seats available here. Please try again."); addBooking(); } else{ AtoCSeat seatSelection = AtoCSeatSearch(); System.out.println("What is the customer's name?"); String customerName = userInput.nextLine(); AtoCSeat.bookSeat(seatSelection, customerName); System.out.println(customerName + "is booked for seat \"INSERT CODE FOR SEAT HERE\""); // note for development later } }
The output of this part of the program is:
The program should stop after the first line is printed in order for customerName to be assigned which would then go into the second printed line.What is the customer's name?
is booked for seat "INSERT CODE FOR SEAT HERE"
Any ideas why this isn't working?
-
Re: Program not waiting for user input
change
toJava Code:int userSelection = userInput.nextInt();
The nextInt() method will grab the int entered, but will not grab the end of line token which will be left dangling, and which will be swallowed by your next call to nextLine(), and this is short-circuiting your program. To solve this, call nextLine() immediately after getting your int (or double) input.Java Code:int userSelection = userInput.nextInt(); userInput.nextLine();
- 11-11-2012, 05:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Program not waiting for user input
The scanner methods nextInt() and nextLine() behave quite differently.
nextInt() first skips anything matching the delimiter (spaces etc) then looks at the input and returns the corresponding int value. Importantly it stops scanning right after the int. Ie the scanner is left pointing at the newline character at the end of the line.
nextLine() returns everything on the current line up to the first newline character it sees. And it skips over that newline so the scanner is left pointing just after the newline character, ready for the next line.
Details in the API docs.
In your case you say "int userSelection = userInput.nextInt();" leaving the scanner just before the newline character on the line where you entered the number. Then later you say "String customerName = userInput.nextLine();" and the scanner will return an empty string (straight away) because it sees the newline at the end of the line with the number.
This is, of course, not what you want. And the solution if you enter a number on a line is to get it with nextInt() but then call nextLine() - throwing away the empty string it returns - so that you are ready for the net line of input.
Java Code:Scanner userInput = new Scanner(System.in); int userSelection = userInput.nextInt(); userInput.nextLine(); // read empty string and throw it away if (userSelection == 1){ if(seatsAvailibleAtoC == 0){ System.out.println("I'm sorry, there are no seats available here. Please try again."); addBooking(); } else{ AtoCSeat seatSelection = AtoCSeatSearch(); System.out.println("What is the customer's name?"); String customerName = userInput.nextLine(); AtoCSeat.bookSeat(seatSelection, customerName); System.out.println(customerName + "is booked for seat \"INSERT CODE FOR SEAT HERE\""); // note for development later } }
Similar Threads
-
User Input???
By jonytek in forum New To JavaReplies: 8Last Post: 01-13-2013, 02:52 PM -
user input on gui
By JoePenguin in forum New To JavaReplies: 5Last Post: 01-26-2012, 07:27 PM -
user input program
By myalani in forum New To JavaReplies: 4Last Post: 10-28-2011, 04:07 AM -
How do I write to an existing file in Java with a program that asks for user input?
By gmoney8316 in forum New To JavaReplies: 13Last Post: 04-16-2010, 02:51 AM -
JFrame created but window contents not showing, othr threads keep waiting on the user
By FezKazi in forum AWT / SwingReplies: 1Last Post: 02-20-2009, 03:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks