What's the best way to check if a String represents an integer in Java?

后端 未结 30 2166
野趣味
野趣味 2020-11-22 05:45

I normally use the following idiom to check if a String can be converted to an integer.

public boolean isInteger( String input ) {
    try {
        Integer.         


        
30条回答
  •  感动是毒
    2020-11-22 06:33

    You can also use the Scanner class, and use hasNextInt() - and this allows you to test for other types, too, like floats, etc.

提交回复
热议问题