Results 1 to 3 of 3
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default 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:

    What is the customer's name?
    is booked for seat "INSERT CODE FOR SEAT HERE"
    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.




    Any ideas why this isn't working?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Program not waiting for user input

    change
    Java Code:
    int userSelection = userInput.nextInt();
    to
    Java Code:
    int userSelection = userInput.nextInt();     
    userInput.nextLine();
    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. user input on gui
    By JoePenguin in forum New To Java
    Replies: 5
    Last Post: 01-26-2012, 07:27 PM
  3. user input program
    By myalani in forum New To Java
    Replies: 4
    Last Post: 10-28-2011, 04:07 AM
  4. Replies: 13
    Last Post: 04-16-2010, 02:51 AM
  5. Replies: 1
    Last Post: 02-20-2009, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •