1

I just started learning Java yesterday, and today I was learning input-taking. I got to know why we need to clear the scanner buffer before taking input using .nextLine() after using .nextInt(), but I noticed one thing if I remove the print statement just before taking the input from .nextLine(). I had to use .nextLine() twice instead of once to remove the buffer otherwise it was not working as intended.

My initial code was this:

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number :");
int num = sc.nextInt();
sc.nextLine();
System.out.println("Enter the string :");
String str = sc.nextLine();
System.out.println(num);
System.out.println(str);

So, if I remove the print statement just before taking the input string and storing it in variable str, it was again passing the value \n to that variable even though I removed the buffer using .nextLine() earlier.

Current code:

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number :");
int num = sc.nextInt();
sc.nextLine();
sc.nextLine();
String str = sc.nextLine();
System.out.println(num);
System.out.println(str);

Ideally, the print statement shouldn't have any effect on the scanner buffer, right? So, why is it skipping it?

Also, since I have already removed the buffer, then why it is again passing the value \n to my variable str unless I either remove the buffer two times or pass the print statement before taking the input.

I tried googling it, and tried searching it a lot on YouTube and here as well, but none of the solutions actually discussed this issue which I am facing right now.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Rohit
  • 11
  • 2
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Franck May 14 '22 at 20:38
  • @Franck Sorry, brother but it did not maybe I wasn't able to frame my ques well. My question actually was that I had to clear my buffer two times instead of one if I will remove the println statement. While this println statement shouldn't have any effect on the scanner buffer so why is it happening so? – Rohit May 14 '22 at 20:43
  • 1
    I cannot reproduce your problem: if I use three `nextLine()` the second input line gets discarded (as expected) - if I use two, the second input line gets printed (after the number is printed) – user16320675 May 14 '22 at 20:58
  • Actually I have copied it and run. The results are quite interesting because I did not face the problem you faced. When I clear it once, I get the correct result and if I do twice, I take an error(no such element exception because it does not find any input). The problem may be because of not rebuilding the project or something like that. – wolfenblut May 14 '22 at 20:58
  • By the way, it may also be because of a copy-paste. In the past, I faced a similar problem. If you copy and paste a text that has a newline char('\n') at the end, it adds another '\n' to the end and you have double of that char("\n\n") which may cause this. check that as well. – wolfenblut May 14 '22 at 21:01
  • @wolfenblut yeah, I guess there is some issue with my IDE because on compiling online it is actually printing the right result. Sorry, for the inconvenience. – Rohit May 14 '22 at 21:05
  • @Rohit Then, could you mark my solution below as the answer? It would be beneficial for others having similar issues. – wolfenblut May 14 '22 at 21:18
  • @wolfenblut Yeah, I will but can you actually check out this short recording of mine. I think then you could actually find what is the issue with my IDE. Sorry, for the inconvenience again. Recoding : https://drive.google.com/file/d/1h7C_MdkMxE7nW9J747gO2gHV2u38r6kH/view?usp=sharing – Rohit May 14 '22 at 21:23

2 Answers2

1

This is completely an interpretation error, made during giving the input.

While you are running your code, you run it in your favorite IDE. One of you (you or the IDE) unintentionally adds another '\n' char to the end according to your cursor's place on the console. This causes the scanner to read that as another line. You can move your cursor by arrow keys (up,down,left,right) on your keyboard as well.

Okay, but why System.out.println(); solves the problem?

Because it moves your cursor to the right place.

Use a different IDE and you'll see that this problem will not happen.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
wolfenblut
  • 131
  • 1
  • 11
  • Thank you very much!!!! It solved my issue. Initially, I was using IntelliJ on the recommendation of someone but when I switched to VS Code after your recommendation the issue got fixed. Again, thank you very much you just solved my headache which was boggling my mind for the last 4 hours. – Rohit May 14 '22 at 21:40
  • This is why [How much patience is required for a software programmer?](https://www.quora.com/How-much-patience-is-required-for-a-software-programmer) is an FAQ :) – wolfenblut May 14 '22 at 21:44
  • True, btw thanks for sharing this xD – Rohit May 14 '22 at 21:56
-1

It's not the matter of print statements or "buffer".

You see, nextInt() method leaves behind a newline character i.e. \n dangling in the input.
Thus, the coming nextLine() method picks this \n as an input and thus,you have to add another nextLine() to capture actual string input.

Solution:
Instead, Use Integer.parseInt(sc.nextLine()) to get int values and use normal sc.nextLine() to get string inputs.

Or add nextLine() to consume this extra \n before the nextLine() to take the actual input.

Prasad Tamgale
  • 325
  • 1
  • 12
  • The questions asks why two times instead of one. – wolfenblut May 14 '22 at 20:55
  • Thanks for the suggestion. I hope I understood you. So, you are suggesting this, right? Scanner sc = new Scanner(System.in); int num = Integer.parseInt(sc.nextLine()); String str = sc.nextLine(); System.out.println(num); System.out.println(str); The input was - 12 hey The expected output was - 12 hey But I am still getting this - 12 (An empty space) – Rohit May 14 '22 at 20:56
  • @Rohit, How are you providing the input? Directly "12 hey" or 12 -> hit enter -> hey? – Prasad Tamgale May 14 '22 at 20:58
  • @PrasadTamgale I am providing input as 12 -> hit enter -> hey – Rohit May 14 '22 at 21:00
  • @Rohit You get the line as a string. Then, you convert it using Integer.parseInt(String) method. – wolfenblut May 14 '22 at 21:04
  • As you now know, what is the role of additional nextLine() we can close the question, right? – Prasad Tamgale May 14 '22 at 21:07