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

    Default Input statement gets skipped

    Hi,

    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?

    Russ

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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 input.next(), 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
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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, 12:53 AM
  2. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09: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, 05:24 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04: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, 08: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
  •