File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Converting Strings to ints and other impracticalities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting Strings to ints and other impracticalities" Watch "Converting Strings to ints and other impracticalities" New topic

Converting Strings to ints and other impracticalities

Aaron Parker

Joined: Jan 31, 2004
Posts: 27
I'm working an an exercise which was assinged to me. I have to use the to accept a numeric value. When I use to put a 1 in a variable which I use to control iterations of a loop, the loop runs 49 times. I've figured out that 1 on the keyboard is equivalent to a decimal 49. My problem is that I can't seem to convert it. I've tried several things to get it from String to int and wrestled with other ideas, but keep running into compiler errors telling me about incompatible types and I can't use this with that, etc. I hope that as I learn this language that all of this seeming bs will be for good reason. Why can't they write a method that converts from what I want to something else I want easily? Anyway, help on getting this decimal 49 to act like a one would be greatly appreciated.
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
I have the simple answer you are looking for. If you are simply doing:
int val =;
you get only the first byte of data from your input line, which in your example will be okay as long as you enter only single digit numbers. The read() method reads in a byte but returns the value as an int (due to type size differences across platforms, I think). You should be able to cast the int you get as a char (character primitive) like this:
char charVal = (char);
which will give you the character representation of your number (1 instead of 49 -- you were right on that part). The next part is a bit wonky (there may be other ways, but this is the one that comes to mind). Take your char and make it into a String, then parse the String into an int.
char charVal = (char);
String strVal = String.valueOf(charVal);
int num = Integer.parseInt(strVal);
and there you have the int you need for your loop. I would do a little cleaning and come up with this, which is the same thing, but more compact:
int num = Integer.parseInt(String.valueOf((char);
Now, if you are going to want more than single-digit numbers for your loop, you will have to read in the entire 'in' stream rather than just the first byte, simply put:
for(int x =; x != -1; x ={
... contatenate the chars as strings to build whole input...
If you do need the whole input stream and the above example does not help, feel free to email me ( and I will give a better explanation.
Hope this helps.

- Chris Stehno, SCPJ
Aaron Parker

Joined: Jan 31, 2004
Posts: 27
That did help. Now I've got a similar problem. I'm using the JOptionPane. As far as I can tell, I have to accept input thru this as a string. I need to convert the one character entered to a char. I tried using a few of the things on the API website (they must assume the user of that resource has some base knowledge, becuase when I look at a lot of that stuff, it makes no sense), but I keep getting compile errors. Is there some greenhorn-friendly list of methods for converting between different datatypes?
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Actually, all of the methods that do common type coercions are on the appropriate wrapper class.
For example, I have a String or character, and I want to convert it to an int? Look on the java.lang.Integer class for parseInt(). If you do that, you quickly see that parseInt() works fine for converting Strings to ints, but not for a character (or char[]). So you need to convert your char or char[] to a String...head over to the String class. String has a constructor that does just what you need, or you could use the valueOf() methods.
Need to go the other direction, say from a double to a String? Check out the String class...valueOf() again. String to double? The Double class has it: parseDouble().
Basically, whenever you're trying to convert to a type, check out the class. If it's a primitive type, like int, double, char, etc, check out the wrapper classes in the java.lang package (Integer, Double, Character, etc, respectively). The constructors, valueOf(), toString(), or parseXxx() methods usually have what you're looking for. For doing array copies and such, check out java.util.Arrays.
How are you supposed to navigate all of this? Well...I hate to say it, but experience. I know about these classes because I started programming in Java from the JDK 1.0, and the libraries just weren't that big back was possible to be relatively conversant in every single class included in them, even up to the JDK 1.2. Now, well, they've grown a bit more. I am personally of the opinion that there's too much in there...I think things like Swing and even most of java.util should be distributed as separate JARs. Alas, they are not.
Just so you understand what's going on with your code, though, let me explain. When you write char c = '1', the variable c actually contains the Unicode value representing 1 (which happens to be the same as the ASCII value, only 16 bits instead of 8). When you directly convert that char to an int, the compiler is giving you the actual, underlying Unicode value...that's where the 49 is coming from. Similarly, if you were to cast an int 1 into a char, if you print that char to screen it'd be some weird control character (or whatever the Unicode value of 1 represents).
So the right way to convert is:

I agree. Here's the link:
subject: Converting Strings to ints and other impracticalities
It's not a secret anymore!