The moose likes I/O and Streams and the fly likes Java input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Java input" Watch "Java input" New topic
Author

Java input

David Lafo
Greenhorn

Joined: Oct 28, 2003
Posts: 3
Hi, im trying to write a java program that allows for conversions to different bases. The conversions aren't the hard part its the input that is giving me trouble. I can only use System.in.read for my input. I was hoping someone could help me and show me how this is done because when i input the number i get the ascii value and i dont know how to convert it from a char to an int. i need up to a five digit number for my input. I was hoping someone could teach me how to do the input properly by casting the number from a char to an int after the input. This would be greatly apprecieated and I thank in advance anyone who can help me with this problem. As you can probably tell im relatively new to java so in other words could you dumb it down for me .
Thankyou
David
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi David,
Welcome to JavaRanch!
Generally, when you're reading from System.in, you want to read one line of input at a time, as a String. Then you do what needs to be done to interpret that String.
To read a line of input, you can use the readLine() method in the java.io.BufferedReader class:

Once you've got a String, you can use the static method Integer.parseInt() to turn it into an integer.


You probably should go read Sun's I/O tutorial (and whatever other sections of the tutorial strike your fancy.)


[Jess in Action][AskingGoodQuestions]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Hey David, welcome to the Ranch!
I wrote a little class that may give you a suggestion as to how to do what you are asking. Run the class, and see what you get. Then you can modify your existing code to fit with the needs of your application.

When you call System.in.read(), you are getting an int back that represents the unicode (or ascii, always get confused with that) number of the character read in. This does this one character at a time. So if you enter 100, that is read as 1, then 0, then 0. You have to grab each character separately then mush them all together. If know that I am going to be getting input that is longer than that, I just deal with it as a String.
Now, if you want to keep with your method, all you have to do is take the input as an int, cast it to a char, then deal with it that way. Something like
char c = (char)System.in.read();
You can also create a byte array and read in data to that, so you could create something like
byte b[] = new byte[5];
int input = System.in.read( b );
for( int i = 0 ; i < b.length ; i++ )
{
System.out.println( (char)b[ i ] );
}
That will display each input character on it's own line.
All sorts of ways to deal with what you need to do!
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
ROFL!
Geez, you type a hell of a lot faster than I do Ernest!
There ya go David, how's that for service!
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Oh, and if you want to convert a char to an int, you can do
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java input