1

I have been learning Java for a little while and feel familiar with using a Scanner Object, but I came across some really confusing behavior.

If I print the result of the nextLine() method twice I get what you'd expect:

Scanner scan = new Scanner(System.in);
System.out.println(scan.nextLine());
System.out.println(scan.nextLine());

Result (> denotes input):

> String 1
String 1
> String 2
String 2

But if I first set the result to a variable and then print it, I get that the second line is missed and I must invoke nextLine() a third time.

Scanner scan = new Scanner(System.in);
String scan1 = scan.nextLine();
String scan2 = scan.nextLine();
String scan3 = scan.nextLine();
System.out.println("scan1: " + scan1 + "\nscan2: " + scan2 + "\nscan3: " + scan3);

Result:

> String 1
> String 2
scan1: String 1
scan2: 
scan3: String 2

I know that when invoking next() or nextInt() and similar methods, it doesn't actually read past the newline character and so when nextLine() is invoked, it reads the newline and ends immediately and then you have to invoke it again to read the whole next line. But this seems different, I mean nextLine() does read the newline character from what I understand, and the only thing I changed was assigning the result of the method to a variable.

I feel very stupid like I'm missing something right in front of me but if anyone knows why your help would be much appreciated. Thanks for reading.

Here is the full code also:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String scan1 = scan.nextLine();
        String scan2 = scan.nextLine();
        String scan3 = scan.nextLine();
        System.out.println("scan1: " + scan1 + "\nscan2: " + scan2 + "\nscan3: " + scan3);
    }
}
John Smith
  • 47
  • 4
  • Does this answer your question? [Java Using Scanner Multiple Times](https://stackoverflow.com/questions/26779393/java-using-scanner-multiple-times) – Ahmed May 31 '22 at 07:16
  • What console do you use? Do you use VM arguments? Is there a notice about `_JAVA_TOOL_OPTIONS`? Newlines (`\n` vs `\r\n`) may be the problem. – dan1st May 31 '22 at 07:20
  • Give this a try here. It works fine, you should make sure that there is only one line that separates the STDIN inputs. [onecompiler](https://onecompiler.com/java/3y5nnpt33) – Mohamad Ghaith Alzin May 31 '22 at 07:22
  • @Ahmed No unfortunately not, I happened to have encountered that before. The difference is I am only using nextLine(), it shouldn't have the same issues, well at least not for the same reasons. – John Smith May 31 '22 at 07:52
  • @dan1st That was a good suggestion, I use Intellij IDEA on Windows. I tried changing it from `\r\n` to `\r` and `\n` but neither changed the result. On the web compiler Mohamad linked the code runs as I would have expected which is interesting. Maybe it is a bug or an issue specific to my computer, either way I can still complete what I was trying to do, it is just that it violated everything I thought I knew. – John Smith May 31 '22 at 08:00
  • I have the most updated version so I would think its not the same thing, however I'm not experienced enough to understand the code in that question so I don't know. – John Smith Jun 01 '22 at 00:53

0 Answers0