How to check if a String is numeric in Java

前端 未结 30 2595
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:26

How would you check if a String was a number before parsing it?

30条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 06:03

    You can use the java.util.Scanner object.

    public static boolean isNumeric(String inputData) {
          Scanner sc = new Scanner(inputData);
          return sc.hasNextInt();
        }
    

提交回复
热议问题