This is generally done with a simple user-defined function (i.e. Roll-your-own "isNumeric" function).

Something like:

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

However, if you're calling this function a lot, and you expect many of the checks to fail due to not being a number then performance of this mechanism will not be great, since you're relying upon exceptions being thrown for each failure, which is a fairly expensive operation.

An alternative approach may be to use a regular expression to check for validity of being a number:

Copypublic static boolean isNumeric(String str) {
  return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
}

Be careful with the above RegEx mechanism, though, as it will fail if you're using non-Arabic digits (i.e. numerals other than 0 through to 9). This is because the "\d" part of the RegEx will only match [0-9] and effectively isn't internationally numerically aware. (Thanks to OregonGhost for pointing this out!)

Or even another alternative is to use Java's built-in java.text.NumberFormat object to see if, after parsing the string the parser position is at the end of the string. If it is, we can assume the entire string is numeric:

Copypublic static boolean isNumeric(String str) {
  ParsePosition pos = new ParsePosition(0);
  NumberFormat.getInstance().parse(str, pos);
  return str.length() == pos.getIndex();
}
Answer from CraigTP on Stack Overflow
Top answer
1 of 16
1055

This is generally done with a simple user-defined function (i.e. Roll-your-own "isNumeric" function).

Something like:

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

However, if you're calling this function a lot, and you expect many of the checks to fail due to not being a number then performance of this mechanism will not be great, since you're relying upon exceptions being thrown for each failure, which is a fairly expensive operation.

An alternative approach may be to use a regular expression to check for validity of being a number:

Copypublic static boolean isNumeric(String str) {
  return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
}

Be careful with the above RegEx mechanism, though, as it will fail if you're using non-Arabic digits (i.e. numerals other than 0 through to 9). This is because the "\d" part of the RegEx will only match [0-9] and effectively isn't internationally numerically aware. (Thanks to OregonGhost for pointing this out!)

Or even another alternative is to use Java's built-in java.text.NumberFormat object to see if, after parsing the string the parser position is at the end of the string. If it is, we can assume the entire string is numeric:

Copypublic static boolean isNumeric(String str) {
  ParsePosition pos = new ParsePosition(0);
  NumberFormat.getInstance().parse(str, pos);
  return str.length() == pos.getIndex();
}
2 of 16
797

With Apache Commons Lang 3.5 and above: NumberUtils.isCreatable or StringUtils.isNumeric.

With Apache Commons Lang 3.4 and below: NumberUtils.isNumber or StringUtils.isNumeric.

You can also use StringUtils.isNumericSpace which returns true for empty strings and ignores internal spaces in the string. Another way is to use NumberUtils.isParsable which basically checks the number is parsable according to Java. (The linked javadocs contain detailed examples for each method.)

🌐
Baeldung
baeldung.com › home › java › java string › check if a string is numeric in java
Check If a String Is Numeric in Java | Baeldung
January 8, 2024 - These methods are also discussed in the Java String Conversions article. Now let’s use regex -?\d+(\.\d+)? to match numeric Strings consisting of the positive or negative integer and floats. It goes without saying that we can definitely modify this regex to identify and handle a wide range of rules. Here, we’ll keep it simple. Let’s break down this regex and see how it works: -? – this part identifies if the given number is negative, the dash “–” searches for dash literally and the question mark “?” marks its presence as an optional one
🌐
Coderanch
coderanch.com › t › 401142 › java › check-String-numeric
How to check if String() value is numeric [Solved] (Beginning Java forum at Coderanch)
If you're using JDK 5 (and if not, why not?) you can use a Scanner instead, which gives you access to ready-made methods which parse numbers of various types (including boolean test methods which allow you to avoid exceptions if that's a problem): ... I also got to this page from a top hit in google. I use the following regex to check whether a string is numeric or not. ((-|\\+)?[0-9]+(\\.[0-9]+)?)+ Valid: 4324 +4123 12321.43 -123.432432 100.00 Invalid: 3243. 32 3232 1231.32131.333 - +-1232134.12 Any text Example: ... I also got here from google. Good pattern but needs an update. Tried to extend it so only one or two decimal places are accepted [0-9]{1,2}+ , and it didnt work.
🌐
Programiz
programiz.com › java-programming › examples › check-string-numeric
Java Program to Check if a String is Numeric
public class Numeric { public static void main(String[] args) { String string = "-1234.15"; boolean numeric = true; numeric = string.matches("-?\\d+(\\.\\d+)?"); if(numeric) System.out.println(string + " is a number"); else System.out.println(string + " is not a number"); } } ... In the above program, instead of using a try-catch block, we use regex to check if string is numeric or not.
🌐
Medium
medium.com › @alxkm › how-to-check-if-a-string-is-numeric-in-java-multiple-approaches-b0515c260812
Java Interview: How to Check if a String Is Numeric in Java-Multiple Approaches | by Alex Klimenko | Medium
August 9, 2025 - Use Double.parseDouble() or Integer.parseInt() for quick checks. Use Regex if you want full control over format. Use Apache Commons Lang if you need to cover many numeric types and edge cases.
🌐
Sentry
sentry.io › sentry answers › java › how to check if a string is numeric in java?
How to check if a String is numeric in Java? | Sentry
public class Main { public static ... true; } catch (NumberFormatException e) { return false; } } } In this example, the isNumeric() method attempts to parse the string using Integer.parseInt(). If the parsing fails, it returns ...
Find elsewhere
🌐
Medium
medium.com › javarevisited › how-to-check-if-a-string-is-numeric-to-avoid-numberformatexception-f07950c47c61
How To Check If A String Is Numeric To Avoid NumberFormatException | by Mouad Oumous | Javarevisited | Medium
April 5, 2023 - We also have a boolean value numeric which stores if the final result is numeric or not. To check if the string contains numbers only, in the try block, we use Double’s parseDouble() method to convert the string to a Double.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 10 › how-to-check-if-string-is-numeric-in-Java.html
How to check if a String is numeric in Java? Use isNumeric() or isNumber() Example
Hence, In the Java application, the simplest way to determine if a String is a number or not is by using the Apache Commons lang's isNumber() method, which checks whether the String is a valid number in Java or not.
🌐
Mkyong
mkyong.com › home › java › java – how to check if a string is numeric
Java - How to check if a String is numeric - Mkyong.com
April 30, 2019 - public static boolean isNumeric(final String str) { if (str == null || str.length() == 0) { return false; } try { Integer.parseInt(str); return true; } catch (NumberFormatException e) { return false; } } ... Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities. ... Only Integer.parseInt(str) works because in other cases, if there are too many digits, it will return true but Integer.parseInt() will still fail.
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-a-number
Java: Check if String is a Number
May 11, 2021 - String is numeric! This method also accepts a String and checks if it's a valid Java number.
🌐
JavaMadeSoEasy
javamadesoeasy.com › 2015 › 12 › how-to-check-string-contains-only_26.html
JavaMadeSoEasy.com (JMSE): How to check string contains ONLY numeric value in Java
Example 3 to check string contains only numeric value in java using org.apache.commons.lang.StringUtils.isNumeric(str) >
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java examples › java: check if string is numeric
Java: Check if String is Numeric - Apps Developer Blog
March 22, 2024 - The isNumeric() method from the StringUtils class is a handy utility that determines whether a given string is numeric. It returns true if the string contains only numeric characters, allowing for leading and trailing whitespaces. Otherwise, it returns false.
🌐
Javaprogramto
javaprogramto.com › 2019 › 04 › java-check-string-number.html
Java Program to Check if a String is Number or contains at least one digit JavaProgramTo.com
Recommend is to isDigit() method gives the best results. while using a regular expression you may see performance issues because the first parser has to be compiled and executed. package examples.java.w3schools.string; public class StringIsNumeric ...
🌐
YouTube
youtube.com › watch
How to Verify if the String Contains only Digits || Java Interview Question - YouTube
How to Verify if the String Contains only Digits! ~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:...
Published   September 23, 2019
🌐
Liberian Geek
liberiangeek.net › home › how-to/tips › how to check if a string is numeric in java?
How to Check if a String is Numeric in Java? | Liberian Geek
February 28, 2024 - You can use the matches() method with a regex pattern like ".*d.*" to check if a Java String contains numeric values.
🌐
Studytonight
studytonight.com › java-examples › how-to-check-if-a-string-is-numeric-in-java
How to Check if a String is Numeric in Java? - Studytonight
String a10c is numeric: false String -104 is numeric: true String 100 is numeric: true String 0xA10 is numeric: false · The isNumeric() method can also be used but it is a little less flexible than the other methods.