• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

integer read using BufferedReader is Lost

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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



I get stuck at all unexpected places while coding

 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner is a lot better and easier to use.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic