This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes integer read using BufferedReader is Lost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "integer read using BufferedReader is Lost" Watch "integer read using BufferedReader is Lost" New topic
Author

integer read using BufferedReader is Lost

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Hello,

This is a simple code. How come the value 'i' be lost?



I get stuck at all unexpected places while coding


www.cs-repository.info
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You need to look through the information about Java available. I would suggest you start with this part of the Java™ Tutorials. You might need a look at the API for BufferedReader and its related classes.

You will find out the read() method does not do what you think it does. It is not at all suitable for reading text, but takes one character at a time. You may need to push enter before you see any results.
Till Stoschus
Greenhorn

Joined: May 28, 2009
Posts: 14
Hi Joe,

reader.read() reads a single character. So, if you enter 1234 +enter, the first number (1) is assigned to i.
I will be 49 because 49 is the ascii-value of 1.

Till
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Thanks all,

My silly mistake. And you are right Till, it spits out the ASCII values.

I solved it with


Is there any other way to do this. I'm writing a program that takes a lot of cmd line inputs. Maybe I could try with Scanner class too...

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Scanner is a lot better and easier to use.
 
 
subject: integer read using BufferedReader is Lost