int num = Integer.parseInt(parseString)
piers
Junior Poster in Training
68 posts since Jul 2007
Reputation Points: 10
Solved Threads: 3
int num = Integer.parseInt(parseString)
which would be correct, if the String to parse was, for instance,
String toParse = "97";
int num = Integer.parseInt(toParse);
but with an input-String like "asu", like in the example he gives us, this will lead to the necessary exceptions, since "asu" is not recognized as an Integer value
stultuske
Posting Sensei
3,110 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 432
use the following code to convert any string to integer.
...
That is absolutelynot the way to convert a String to an int.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
If you read the posts above, it has already been answered.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
97 is the byte value of 'a'. Your read() call just got a single byte and converted it to int.
In any case, it's irrelevant. Without a stated encoding expectation, "asu" cannot be converted to int. Integer.parseInt() will convert a valid integer string to an int value. If you want to convert some other string to an int, there are many different ways to do so, but they are specific encoding methods - not just a general api call or statement.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
even if you write Integer.parsrInt("asu"); you will get Number format exception but if you write Integer.parseInt("197") it will give the number 197.
Of course, which is already mentioned. You are missing the entire point.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Integer.parseInt() was already explained. The real point is that there is no generic method to make "asu" an int. It is totally dependent upon how you need to encode the string and that is totally dependent on what you intend to use it for.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
You could, but what would the point be?
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
You can write whatever you want if this is the way you want to convert "asu" to a String. As it was said in the first reply what do you want to convert "asu" to?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448