• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please tell me what is wrong with read method(or the way i am using it).

 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am asking user for an input using BufferedReader's read and it must return integer,
it is returning an int but the value is different,below is the code

output of a user input is here
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read() reads a byte, and returns that byte in an int.

So if the byte is has the value of 65 (0x41), the int will be 65. If the byte has a value of 49 (0x31), that int will be 49, and so on.

Now, run this, and see if you can understand what's happening:


Then go back and read the docs for BufferedReader again.
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply,But I am not getting it,
tell me what should i do if i want to read an integer using read() method
and to store it with the same value.
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:Thanks for your reply,But I am not getting it,
tell me what should i do if i want to read an integer using read() method
and to store it with the same value.

If you look at the API documentation, you'll see that you can't (or at least not without some work), because BufferedReader's read() method returns a character.

I think what you need to do is to describe what you do want, because there are loads of alternatives (and my suspicion is that you want the user to type something in (a string or a bunch of characters), which you then convert to a number).

But please tell if I'm wrong.

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:Thanks for your reply,But I am not getting it,
tell me what should i do if i want to read an integer using read() method
and to store it with the same value.


Basically, from what you have done -- you can't. The input is a stream of characters, which is delivered to as ASCII codes. You need to read all the codes (the characters) to form a string, and then parse that string to an int.... of course, you can use the java.util.Scanner class on the input stream which can do the integer read for you.

Henry
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Nirvikalp Rao wrote:Thanks for your reply,But I am not getting it,
tell me what should i do if i want to read an integer using read() method
and to store it with the same value.

If you look at the API documentation, you'll see that you can't (or at least not without some work), because BufferedReader's read() method returns a character.

I think what you need to do is to describe what you do want, because there are loads of alternatives (and my suspicion is that you want the user to type something in (a string or a bunch of characters), which you then convert to a number).

But please tell if I'm wrong.

Winston

Yes you are right.I want to ask user to give some value (numeral) and then i would use those values as an input to mathematical equations,so i wanted them to be int or convertible to int.
sorry for late response
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Basically, from what you have done -- you can't. The input is a stream of characters, which is delivered to as ASCII codes. You need to read all the codes (the characters) to form a string, and then parse that string to an int.... of course, you can use the java.util.Scanner class on the input stream which can do the integer read for you.

Henry

thanks Henry I got it.
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:Yes you are right.I want to ask user to give some value (numeral) and then i would use those values as an input to mathematical equations,so i wanted them to be int or convertible to int.
sorry for late response

No problems.

My advice: look at the readLine() method. That accepts a line of text from the user as a String. You still then have to convert it to a number (and you should also allow for the fact that they might make a mistake).

Further advice: once you've got that code working, stick it in a method in a utility class somewhere, because you will need it again.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also have a look at the Java Tutorials. Look for the section about “file IO”, which will probably tell you all you need to know.
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
No problems.

My advice: look at the readLine() method. That accepts a line of text from the user as a String. You still then have to convert it to a number (and you should also allow for the fact that they might make a mistake).

Further advice: once you've got that code working, stick it in a method in a utility class somewhere, because you will need it again.

Winston

yeah ,now i think using "readLine" or scanner is a good idea.
Thanks Winston.
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:Thanks Winston.

You're welcome.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:
yeah ,now i think using "readLine" or scanner is a good idea.
Thanks Winston.


Okay, now, to close the loop, going back to your problem statement:



Do you understand why you got the output "56" there?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic