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 Please tell me what is wrong with read method(or the way i am using it). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please tell me what is wrong with read method(or the way i am using it)." Watch "Please tell me what is wrong with read method(or the way i am using it)." New topic
Author

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

Nirvikalp Rao
Greenhorn

Joined: Feb 14, 2012
Posts: 27

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 14, 2012
Posts: 27

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

Joined: Mar 17, 2011
Posts: 7898
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


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

Joined: Feb 14, 2012
Posts: 27

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

Joined: Feb 14, 2012
Posts: 27

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

Joined: Mar 17, 2011
Posts: 7898
    
  21

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

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

Joined: Feb 14, 2012
Posts: 27

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

Joined: Mar 17, 2011
Posts: 7898
    
  21

Nirvikalp Rao wrote:Thanks Winston.

You're welcome.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please tell me what is wrong with read method(or the way i am using it).