Strange Integer.parseInt exception thrown

Exception in thread "Thread-2" java.lang.NumberFormatException: for input line: "3"

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

This is the error I get when I try to convert "3" to an integer. Well, I get this β€œ3” through the RS-232 port, so maybe that is what causes the error.

If anyone knows what might be causing this, it will be appreciated.

+4
source share
2 answers

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

Try result[0].trim()

+6
source

Take a look at the char values ​​of result[0] when this happens. Perhaps this β€œ3” is not really an ASCII character β€œ3”, but some weird Unicode character that looks just like 3.

+1
source

Source: https://habr.com/ru/post/1304824/


All Articles