This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error with Scanner class reading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error with Scanner class reading" Watch "Error with Scanner class reading" New topic
Author

Error with Scanner class reading

RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
Try this old post.
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 26, 2010
Posts: 200
    
    1

Well, it solves Raj's issue as of now... And anyways shouldn't autoboxing take care of getting me an int.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 26, 2010
Posts: 200
    
    1

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

Joined: Oct 13, 2005
Posts: 39084
    
  23
On "beginning" we try to teach people the most elegant way to do things.
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

gotcha... will keep that in mind... Have a good one!
Aditya Mehta
Greenhorn

Joined: Dec 20, 2010
Posts: 10
try using "scan.next()" instead of nextLine()


OCPJP 6
 
GeeCON Prague 2014
 
subject: Error with Scanner class reading