Results 1 to 3 of 3
  1. #1
    rjkfsm is offline Member
    Join Date
    May 2011
    Rep Power

    Default Input statement gets skipped


    I have the following code in a loop:

    System.out.println("What is the name of the department?");
    s_Temp = input.nextLine();

    The first time the command is executed, it works properly. Subsequent executions cause the 2nd line to be skipped and s_Temp becomes an empty string.

    Anyone know why?


  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    It might help to see the rest of the code, but let me guess, "input" is a Scanner and later in the loop is another question and there you are doing, or input.nextInteger(), etc, etc, but not input.nextLine(), right? Well, in that case, that call will leave the "newline" in place and the next call to nextLine() will simply return the rest of the line from that previous call (even if that is just an empty string). Add a call to nextLine after the other "next" call.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    I'm guessing, but I think that your problem may lie elsewhere, that you are using the input Scanner object elsewhere in your code and calling something other than input.nextLine() and thereby not properly handling an end of line token. You may want to call input.nextLine() after obtaining an int or double from the Scanner to swallow this token. If this doesn't help, I suggest you post more of your code, but please use code tags when doing so (see the link in my signature below).

    edit: I'm a bit slow today, it seems!

Similar Threads

  1. The if Statement, need some help!
    By Keno777 in forum New To Java
    Replies: 6
    Last Post: 10-24-2009, 01:53 AM
  2. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 10:26 AM
  3. Can't get my "if" statement to read user input
    By daletron3030 in forum New To Java
    Replies: 7
    Last Post: 01-16-2009, 06:24 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 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