Results 1 to 3 of 3
  1. #1
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default reading nextLine after a nextInt not working

    Hopefully, this is a simple one. For some reason, after the numberOfRooms is entered (int # followed by a carriage return), the routine prints out the "Please enter the name of the room: " string, but does not wait for a response before going on to "Please enter the length of " statement. Any ideas on what's happening? When I change the order of the reads to have the two strings read first, it works. Do I need to clear something after reading an int?

    Java Code:
      
    Scanner keyboard = new Scanner(System.in);
        
    System.out.print ("Please input the house name: ");
    houseName = keyboard.nextLine();
            
    System.out.print("How many rooms will be in this house? ");
    numberOfRooms = keyboard.nextInt();
            
    System.out.print("Please enter the name of the room: ");
    roomName = keyboard.nextLine();
    
    System.out.print ("Please enter the length of " + roomName + ": ");
    roomLength = keyboard.nextDouble();

  2. #2
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: reading nextLine after a nextInt not working

    Fubarable's Avatar
    Fubarable posted a solution to essentially insert a keyboard.nextLine() on line 8. This does fix the problem, but makes me feel like I've used bubble gum to plug an oil leak. Any ideas what causes this problem and what needs to be done to fix it? Since I don't know any other way to input characters, I am stuck with the nextLine method for the moment.
    Last edited by Jadeclan; 11-04-2011 at 04:09 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: reading nextLine after a nextInt not working

    The problem is that nextInt() does not handle the end of line token, and even though it handles the int that's been entered, there's still an end of line token that is dangling and that has not yet been handled. When nextLine is called, it swallows this token immediately before the user gets a chance to enter anything. The same issue occurs with your nextDouble at the end of your code.

Similar Threads

  1. nextInt() repeat
    By trishtren in forum New To Java
    Replies: 3
    Last Post: 10-09-2011, 07:20 PM
  2. Replies: 1
    Last Post: 09-30-2011, 01:13 AM
  3. Reading nextInt() then nextLine() form a file.
    By Mokomi in forum New To Java
    Replies: 9
    Last Post: 09-06-2011, 09:41 AM
  4. nextint() Method !
    By M7MD in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-25-2010, 07:13 AM
  5. Very Bad problem with nextInt()
    By usiyalla in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 02:44 AM

Posting Permissions

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