티스토리 뷰

자바 문자열이 숫자인지 확인하기

  • Double.parseDouble

    public class NumbericYn {
      public static void main(String[] args) {
          String str = '123.0';
          boolean numberYn = parseDouble(str);
          System.out.println(numberYn);
      }
    
      public static boolean numbericYn(String str) {
          try {
              Double.parseDouble(str);
              return true;
          } catch (NumberFormatException ex) {
              return false;
          }
      }
    }
  • Apache Commons Lang

    public class NumbericYn {
      public static void main(String[] args) {
          String str = '123.0';
          boolean numberYn = parseDouble(str);
          System.out.println(numberYn);
      }
    
      public static boolean numbericYn(String str) {
          return NumberUtils.isNumber(str);
      }
    }
댓글