0

I am fairly new to Java and getting user input is proving to be difficult and the teacher didn't really explain it very well, and all resources I used didn't help with my problem:

I am trying to write a simple program that continues to read user input and stores it in a Queue (-> what the program is actually about) until a certain String is entered. First it takes two Strings and then two doubles. The first time everything works fine but with the second iteration for some reason it skips the second String input and goes straight to the first double input.

Here is an example code:

    System.out.print("Enter first string: ");
    while(!line.equals("-") && scanner.hasNext())
    {
        line = scanner.nextLine();
        if(!line.equals("-"))
        {
            a.offer(line);

            System.out.print("Enter second string: ");
            if(scanner.hasNextLine())
            {
                line = scanner.nextLine();
                b.offer(line);
            }
            System.out.print("Enter first double: ");
            double tmp = scanner.nextDouble();
            c.offer(tmp);

            System.out.print("Enter second double: ");
            tmp = scanner.nextDouble();
            d.offer(tmp);
        }
        System.out.print("Enter first string: ");
    }

I have only programmed in C up until now and getting user input was way easier there. So what am I doing wrong?

EDIT: It was marked as duplicate of Skipping nextLine() after using next(), nextInt() or other nextFoo() methods I looked at it and in that question the problem was that the newline character at the end wasn't consumed due to the use of .nextInt(). But I am using a .nextLine() which should consume the newline, so I still don't get why it's happening.

  • "*But I am using a .nextLine() which should consume the newline, so I still don't get why it's happening.*" in your loop first instruction `line = scanner.nextLine();` may be executed after last instruction from previous iteration `tmp = scanner.nextDouble();` which means that line separator will not be consumed causing `line` to store empty string. – Pshemo Dec 02 '15 at 18:34
  • @Pshemo Thanks, I just added a .nextLine() at the end of the loop and that did the trick! But I am still a little confused as to why it was the second .nextLine() in the loop that was skipped and not the first. Because as you said the .nextDouble didn't consume the \n, if that was still in the buffer so shouldn't the first .newLine() have caught that and the second be executed normally? –  Dec 02 '15 at 18:45
  • Lets assume that your code looks like `Scanner sc = new Scanner(System.in); while(condition){String line = sc.nextLine(); double value = sc.nextDouble();}`. Now your data is `foo bar\n123\nbaz bam\n321`. In first iteration when you execute `sc.nextLine()` you will consume `foo bar\n` and method will return `foo bar`. After it you execute `nextDouble` and your method will consume `123` but not `\n`. So when you go to next iteration and you call `nextLine` `\n` is consumed and empty string is returned and later `nextDouble` will try to read `baz` which will throw exception. – Pshemo Dec 02 '15 at 18:50
  • So that was general idea about problem with scanner and its `nextLine` after any of other `nextFoo` methods. So as you see problem appeared in second loop because of not consumed `\n` by `nextDouble` which was last instruction in our loop. I am not sure what is confusing you precisely in your example. Maybe try printing returned values to see what is happening with input. – Pshemo Dec 02 '15 at 18:55
  • @pshemo Ah sorry for not being clear. I have two .nextLine() in my loop, one as the very first statement in the loop, and one that is after "Enter second string" (line 12). This is the one that is being skipped. Starting from the second iteration. –  Dec 02 '15 at 19:01
  • It is strange that you see any problems then (unless you are not entering `if` statement which means additional `nextLine` is not necessary so it should be placed right after `nextDouble` rather than right at the end of loop). Also you shouldn't probably put `System.out.print("Enter first string: ");` at end of loop. I suspect that placing it at start of loop and removing that syso before loop would be better. Anyway I can't help you much without seeing real code example and input. – Pshemo Dec 02 '15 at 19:25

0 Answers0