J
jt
<Question>
Why does the NumberFormatException get thrown when the String argument
is not empty?
</Question>
<Background>
I was reading the Sun Tutorial and happened across the section on integers:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
I saw the bit on octal and hex representations and thought that I might
be able to take a string containing..oh say "O23" and use the
Integer.parseInt method to convert it to an integer. After all, the
tutorial does say
The prefix 0 indicates octal, whereas 0x indicates hexadecimal.
int decVal = 26; // The number 26, in decimal
int octVal = 032; // The number 26, in octal
int hexVal = 0x1a; // The number 26, in hexadecimal
Here's my code:
package just_playing_around;
public class WillThisWork {
public static void main(String [] args) {
String s = "O23";
int x = Integer.parseInt(s.trim());
System.out.println("s as a string is " + s);
System.out.println("s as an integer is " + x);
}
}
And here's the error I get when I run it
Exception in thread "main" java.lang.NumberFormatException: For input
string: "O23"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at just_playing_around.WillThisWork.main(WillThisWork.java:6)
</Background>
<Research>
1) I did a google search on cljp and found the following post
http://groups.google.ca/group/comp....Unknown+Source)&rnum=7&hl=en#d7610d111ecc7ceb
If you will notice Patricia's post
I think this is the critical data. It claims that there was an
Integer.parseInt call on line 72 with the empty string as input.
Does that make sense?
Patricia
2) I tried to eliminate the probable causes of the "empty string" in
the following sequence
2a) I removed the parseInt and print for x from my code.
package just_playing_around;
public class WillThisWork {
public static void main(String [] args) {
String s = "O23";
// int x = Integer.parseInt(s.trim());
System.out.println("s as a string is " + s);
// System.out.println("s as an integer is " + x);
}
}
s as a string is O23
2b) In the same program, I did the trim and assigned the value to a new
string so that I could see what the result of the string was without
disturbing the original. I also added a comparison to see if indeed
both strings were the same.
package just_playing_around;
public class WillThisWork {
public static void main(String [] args) {
String s = "O23";
String y = s.trim();
System.out.println("s as a string is " + s);
System.out.println("y as a string is " + y);
if (s.equals(y))
{
System.out.println("They are the same");
}
else
{
System.out.println("They are different");
}
}
}
s as a string is O23
y as a string is O23
They are the same
<Research>
<Theory>
Either the runtime error I am getting is misleading (see Research.1> or
the runtime error is pointing me to something else, something more
insidious.
</Theory>
<Followup>
Where am I going wrong? Did I mis-read something somewhere or is what I
am trying to do just not do-able using the tools I have available to me
right now?
</Followup>
Thanks for your help
Why does the NumberFormatException get thrown when the String argument
is not empty?
</Question>
<Background>
I was reading the Sun Tutorial and happened across the section on integers:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
I saw the bit on octal and hex representations and thought that I might
be able to take a string containing..oh say "O23" and use the
Integer.parseInt method to convert it to an integer. After all, the
tutorial does say
The prefix 0 indicates octal, whereas 0x indicates hexadecimal.
int decVal = 26; // The number 26, in decimal
int octVal = 032; // The number 26, in octal
int hexVal = 0x1a; // The number 26, in hexadecimal
Here's my code:
package just_playing_around;
public class WillThisWork {
public static void main(String [] args) {
String s = "O23";
int x = Integer.parseInt(s.trim());
System.out.println("s as a string is " + s);
System.out.println("s as an integer is " + x);
}
}
And here's the error I get when I run it
Exception in thread "main" java.lang.NumberFormatException: For input
string: "O23"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at just_playing_around.WillThisWork.main(WillThisWork.java:6)
</Background>
<Research>
1) I did a google search on cljp and found the following post
http://groups.google.ca/group/comp....Unknown+Source)&rnum=7&hl=en#d7610d111ecc7ceb
If you will notice Patricia's post
I think this is the critical data. It claims that there was an
Integer.parseInt call on line 72 with the empty string as input.
Does that make sense?
Patricia
2) I tried to eliminate the probable causes of the "empty string" in
the following sequence
2a) I removed the parseInt and print for x from my code.
package just_playing_around;
public class WillThisWork {
public static void main(String [] args) {
String s = "O23";
// int x = Integer.parseInt(s.trim());
System.out.println("s as a string is " + s);
// System.out.println("s as an integer is " + x);
}
}
s as a string is O23
2b) In the same program, I did the trim and assigned the value to a new
string so that I could see what the result of the string was without
disturbing the original. I also added a comparison to see if indeed
both strings were the same.
package just_playing_around;
public class WillThisWork {
public static void main(String [] args) {
String s = "O23";
String y = s.trim();
System.out.println("s as a string is " + s);
System.out.println("y as a string is " + y);
if (s.equals(y))
{
System.out.println("They are the same");
}
else
{
System.out.println("They are different");
}
}
}
s as a string is O23
y as a string is O23
They are the same
<Research>
<Theory>
Either the runtime error I am getting is misleading (see Research.1> or
the runtime error is pointing me to something else, something more
insidious.
</Theory>
<Followup>
Where am I going wrong? Did I mis-read something somewhere or is what I
am trying to do just not do-able using the tools I have available to me
right now?
</Followup>
Thanks for your help