This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2012/11/27

NumberFormat exception

erdelf erdelf

2012/11/27

#
I get this error: Exception in thread "main" java.lang.NumberFormatException: For input string: "rect x=" in this line:
int a = Integer.parseInt(read_x);
read_x is a string. help required
mjrb4 mjrb4

2012/11/27

#
The string that you're parsing needs to consist only of digits (0-9), because otherwise it makes no sense as an integer. You're trying to convert "rect x=" into a number, hence the error. I presume from your other thread you're probably calculating the substring the wrong way round - I would imagine you need the bit after "rect x=" to convert to an integer (though this is just a guess.)
erdelf erdelf

2012/11/28

#
thx for the help. Now it is really interesting, same line, changed the substring, and got this error: Exception in thread "main" java.lang.NumberFormatException: For input string: "0 "
danpost danpost

2012/11/28

#
Looks like there is an extra space after the zero in the string.
erdelf erdelf

2012/11/28

#
well, if I short the substring at the end, it returns the same error with the input string " "
danpost danpost

2012/11/28

#
try using the 'trim' method: " string ".trim() becomes "string"
mjrb4 mjrb4

2012/11/28

#
erdelf wrote...
well, if I short the substring at the end, it returns the same error with the input string " "
That's probably because you're incorrectly changing the arguments to substring - you're probably adding one to the first parameter (or using the one parameter version of substring), rather than taking one from the second parameter. Doing so should remove the space. Alternatively, danpost's suggestion of using trim() should work just as well.
erdelf erdelf

2012/11/29

#
@danpost, I will try the trim method, as soon as I am home. @mjrb4, I am using the two parameter version, and changed the second parameter
erdelf erdelf

2012/11/29

#
trim doesnt seem to work, i still get the error: Exception in thread "main" java.lang.NumberFormatException: For input string: "0 "
mjrb4 mjrb4

2012/11/29

#
Can you post the exact block of code you're using? Non-breaking spaces aside, trim *will* remove whitespace from the end of a string if used correctly.
erdelf erdelf

2012/11/30

#
thank you for your help this works now; I was dumb and wrote
read_x.trim();
instead of
read_x = read_x.trim();
You need to login to post a reply.