Proper way to avoid parseInt throwing a NumberFormatException for input string: “”

后端 未结 7 879
小鲜肉
小鲜肉 2020-12-06 09:53

When I run parseInt:

Integer.parseInt(myString);

it throws:

NumberFormatException: For input string: \"\"

7条回答
  •  时光说笑
    2020-12-06 10:38

    If the string can be empty I do it this way:

    Integer.parseInt("0" + inputString)
    

    When I'm not sure it contains only digits:

    Integer.parseInt(0 + inputString.replaceAll("\\D+",""))
    

提交回复
热议问题