Check if a String is a Number Java

Check if a String is a Number Java | The string is a sequence of characters, usually, when we use a string, in some cases, we also denote an integer or number in the form of a string. This blog helps in Java how to check if a string is a number.

For Example:-
1. String= “12344”
Yes, it is a number.
2. String = “KnowProgram”
No, it is not a number.
3. String = “123 Hello”
No, it is not a number.

Our program works as shown in the above example. Let us see how to check if a string is number in Java using different methods.

Program to Check if a String is a Number Java

There are many ways to check whether the string is a number or not but for instance, we are now using the parseDouble() method which is a method of Double class. This method takes the string as a parameter and parses the same to a double value and returns the string containing a valid number.

This parseDouble() method throws NumberFormatException if the input string has an invalid number string. Let us see Java how to check if a string is a number through the program.

public class Main {
   public static void main(String[] args) {
      String[] string = 
         { "46", "100.0", "-99", "100L", "0.9", 
            "Hello", "Hi123", "123.2.3", "12Hi12" };

      for (String stringNum : string) {
         System.out.println(stringNum + ": " 
                        + isNumber(stringNum));
      }
   }

   private static boolean isNumber(String str) {
      boolean number = false;
      try {
         Double.parseDouble(str);
         number = true;
      } catch (NumberFormatException nfe) {}
      return number;
   }
}

Output:-

46: true
100.0: true
-99: true
100L: false
0.9: true
Hello: false
Hi123: false
123.2.3: false
12Hi12: false

If we want to check whether a string is a specific primitive data type or not then we can use parseX() method. All wrapper classes contain parseX() method which can be used to convert strings to that specific primitive data type.

All below methods are used to convert String to primitive data type and throw NumberFormatException if the string does not contain a parsable value.

  1. parseByte()
  2. parseInt()
  3. parseLong()
  4. parseFloat()
  5. parseDouble()

Java Check if a String is a Number using Regular Expression

The next way is by using regular expressions. The regular expression is used to find the particular pattern of the characters. This is one of the easiest ways to check if a string is a number Java.

public class Main {
   public static void main(String[] args) {
      String[] string = 
         { "46", "100.0", "-99", "100L", "0.9", 
            "Hello", "Hi123", "123.2.3", "12Hi12" };

      for (String stringNum : string) {
         System.out.println(stringNum + ": " 
                        + isNumber(stringNum));
      }
   }

   private static boolean isNumber(String string) {
      if (string == null)
         return false;
      else
         return string.matches("^(-)?\\d+(\\.\\d+)?$");
   }
}

Output:-

46: true
100.0: true
-99: true
100L: false
0.9: true
Hello: false
Hi123: false
123.2.3: false
12Hi12: false

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *