问题
Exception in thread "main" java.lang.NumberFormatException: For input string: " 400"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:514)
at Library.loadBooks(Library.java:191)
at UseLibrary.main(UseLibrary.java:102)
what's the problem?
回答1:
You have a whitespace in " 400"
that is resulting in a NumberFormatException
.
Just use String.trim()
before trying to parse.
Read the documentation for more information on String.trim()
.
回答2:
Use .trim() function to eliminate spaces.
Integer.valueOf(" 400".trim());
That function would avoid you this kind of problems.
来源:https://stackoverflow.com/questions/11085144/numberformatexception-when-attempting-to-parse-string-as-an-integer