NumberFormatException when attempting to parse string as an integer

我是研究僧i 提交于 2019-12-13 22:30:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!