• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error with Scanner class reading

 
RajTilak Sivaluri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,
In the below mentioned code which is getting executed with no compile time errors or Runtime exceptions but i have sum problem when i am reading the date after reading the roll in the program it is not reading the category . At run time it is directly asking the input for proid variable . please can anyone explain the reason


Exactly the output looks this way :

Enter roll:
446
roll is :446
Enter category:
Category is:

Enter prodid




Here comes the problem immediately after reading the value for roll it is ignoring category .
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RajTilak Sivaluri wrote:
Here comes the problem immediately after reading the value for roll it is ignoring category .


Basically, the nextInt() method reads characters until it is no longer a digit; The nextLine() method returns all characters to the EOL character. When you use them both, this this fashion, the nextInt() will swallow up all the digits, but nothing more. And the nextLine() will swallow the EOL character that you entered, when you typed in the integer.

Henry
 
RajTilak Sivaluri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could understand that the enter which i press after taking the input for roll is being treated as a character for the next string input . Can any one give the wise way of solving this problem
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this old post.
 
Sai Hegde
security forum advocate
Ranch Hand
Posts: 210
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When Scanner has methods like nextInt(), is there any use in using Integer#valueOf? That creates an Integer, not an int, which has to be un-boxed.
 
Sai Hegde
security forum advocate
Ranch Hand
Posts: 210
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it solves Raj's issue as of now... And anyways shouldn't autoboxing take care of getting me an int.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean you prefer using parsing and boxing over a method which returns an int directly? That way you are never going to tackle this little peculiarity of the Scanner class, which I don't remember seeing in any of the books. Henry has explained it, so you can see it is quite simple once you get the hang of it.
 
Sai Hegde
security forum advocate
Ranch Hand
Posts: 210
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method does return an int directly... and the explanation that Henry provides is good as well... What I provided was just a hack to swallow the return.
And anyways what works never needs to be the most logical way of doing things.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On "beginning" we try to teach people the most elegant way to do things.
 
Sai Hegde
security forum advocate
Ranch Hand
Posts: 210
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotcha... will keep that in mind... Have a good one!
 
Aditya Mehta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using "scan.next()" instead of nextLine()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic