How to check if a string is a number in Java

There are several ways to check is a string is a number or not. A few of them are discussed below.

Checking if a string is a number using parsing

Parsing in Java is to read the value of one object to convert it to another type. For example, parseInt converts the string into its equivalent integer.

In this method the string is first parsed to Double, Integer, or Float. Then we will check if it throws an error or not. If it throws an error then the string is not a number else it is a number.

public class Example {
  public static void main(String[] args) {
    String str = "35625.9738";
    boolean num = true;
    try {
      Double res = Double.parseDouble(str);
    } catch (NumberFormatException e) {
      num = false; // if str is not a number num is set false
    }
    if (num)
      System.out.println(str + " is a number");
    else
      System.out.println(str + " is not a number"); 
  }
}

The output is

35625.9738 is a number

Checking if a string is a number using regular expression

In this method the String's matches method is used to check if a string is a number.

The syntax of the regular expression is

-?\\d+(\\.\\d+)?

Here

  • -? - matches zero or more negative numbers
  • \\d+ - it checks for one or more numbers
  • (\\.\\d+)? - \\. checks for decimal numbers(float or double) ,
  • \\d+ - if yes it must be followed by one or more numbers

Using this method we can check for positive and negative whole numbers and decimals.

public class Test {
  public static void main(String[] args) {
    String str = "-72927.02828";
    boolean num = true;
    num = str.matches("-?\\d+(\\.\\d+)?");
    if (num)
      System.out.println(str + " is a number");
    else
      System.out.println(str + " is not a number");
  }
}

The output is

-72927.02828 is a number

Checking if a string is a number using isDigit()

One can check is a string is a number using isDigit() method. The isDigit() method takes character as a parameter and returns true if the character is number else returns false.

The drawback of this method is that it is applicable only to the whole numbers.

To use this method the string is first converted to character array. Then the array is iterated and checked if is a number or not.

public class Demo {
  public static void main(String[] args) {
    boolean res = false;
    String input = "985632";
    if (input != null && !input.isEmpty()) {
      for (char c: input.toCharArray()) {
        res = Character.isDigit(c);
        if (!res) {
          break;
        }
      }
    }
    if (res) {
      System.out.println(input + " is a number");
    } else {
      System.out.println(input + " is not a Number");
    }
  }
}

The output is

985632 is a number

Checking if a string is a number using if condition and for loop

We can check if a string is a number using if condition and for loop. In this method, first the string is converted to character array and then each character is iterated and checked if it is a number using if condition.

This method detects whole number and negative number but not decimal numbers.

public class Example {
  public static boolean Check(String str) {
    if (str == null)
      return false;
    char[] arr = str.toCharArray();
    int i = 0;
    if (arr[0] == '-' && arr.length > 1)
      i = 1;
    for (; i < arr.length; i++) {
      if (arr[i] < '0' || arr[i] > '9')
        return false;
    }
    return true;
  }
  public static void main(String[] args) {
    String str = "297543";
    boolean res = Check(str);
    if (res) {
      System.out.print(str + " is a number.");
    } else
      System.out.print(str + " is not a number.");
  }
}

The output is

297543 is a number.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions