How to check if a String is a number in Java?

To check if a String is a number in Java, call Double.parseDouble and catch NumberFormatException.

Here's how you do it:

public class IsNumber {

  public static void main(String[] arg) {
    System.out.println(isNumeric("4.32")); // true
    System.out.println(isNumeric("5")); // true
    System.out.println(isNumeric("Hello World!")); // false
  }

  public static boolean isNumeric(String string) { 
    try {  
      Double.parseDouble(string);  
      return true;
    } catch (NumberFormatException e) {  
      return false;  
    }  
  }
}