• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anomaly while using the Scanner class for taking inputs  RSS feed

 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Everyone

There is a problem that I am facing while using the Scanner class for taking inputs from the user. Suppose if I need to take an integer and a String input from the user, and I take the integer input first then the place where the user has to input the String skips and the variable shows a "" nothing on printing it. Suppose in this code snippet
The output is
Enter a number
13
Enter a word
Enter integer is 13
Entered statement is

I need to know why this happens and how should I avoid it. I've seen that taking the String input first solves the issue but while taking the input in a loop to populate a database the program only lets the user enter the first statement and the following statements are skipped. Is it something I'm doing wrong?

Regards,
Ranajoy
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by searching my posts for “Beatles”. You will find this which addresses exactly the same problem. Read that post, and if you still need more information, don't hesitate to ask again.
 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick reply! Okay, so it is the new line character or the enter character that is getting added to the string which the user enters and the nextInt() function reads till the new line character. The solution as you specified is to discard the new line character entered by using a nextLine() statement. I've understood it pretty well thanks to you! Are there any other classes besides the BufferedReader and Scanner class that is efficient enough to do these all by themselves and the programmer is relieved of some burden? P.S - I am very sorry that I did not search very well for the solution in this forum. Actually the problem was that your post addressing to the issue was under a different topic altogether "explanation of getter/setter methods -- what's the difference exactly?" and thus I was unable to find the correct results. Sorry for increasing you pain to search for that post again and referring to it here.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done working out my old post
I would suggest you write yourself a utility class for keyboard input. Give it a private constructor, and all static methods including a private final Scanner field pointing to System.in. You can see a suggested method to read the next whole line here.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware!
There is a serious error in the post I gave you a link to. You must read from that post to the end of the thread.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranajoy Saha wrote: . . . nextInt() function reads till the new line character. . . .
Yes, but it stops just before the line‑end characters, assuming there is nothing after the number. That is because line‑end counts as whitespace, and you know what the default delimiter for a Scanner is …
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!