| Author |
integer read using BufferedReader is Lost
|
John Eipe
Ranch Hand
Joined: May 23, 2008
Posts: 205
|
|
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: 32668
|
|
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: 205
|
|
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: 32668
|
|
|
Scanner is a lot better and easier to use.
|
 |
 |
|
|
subject: integer read using BufferedReader is Lost
|
|
|