Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Integer.parseInt exception

Tags:

java

Exception in thread "Thread-2" java.lang.NumberFormatException: For input string: "3"

 int test = Integer.parseInt(result[0]);

This is the error I keep getting when I'm trying to convert "3" to an integer. Well I'm receiving this "3" through a RS-232 port, so maybe this is what is causing the error.

If anybody has any idea what could be causing this it would be appreciated.

like image 825
Albinoswordfish Avatar asked Feb 28 '23 08:02

Albinoswordfish


2 Answers

What is the data type of result[0]? If it's a string, are you sure there are no spaces or new lines around it?

Try result[0].trim()

like image 85
Kapil D Avatar answered Mar 07 '23 08:03

Kapil D


Take a look at the char values of result[0] when this occurs. It might be possible that this "3" is in fact not the ASCII character '3' but some strange Unicode character that just looks like a 3.

like image 20
Michael Borgwardt Avatar answered Mar 07 '23 07:03

Michael Borgwardt