• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java input

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and if you want to convert a char to an int, you can do
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic