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

Something like:

public 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:

public 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:

public 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:

public 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:

public 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:

public 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
Discussions

How to check if a string is numeric in Java?
Best way would be to use a regex to validate if the string belongs to numeric pattern. Depending on handling negatives or float, you need to use the appropriate regex. Below link has all the possible combinations of regex https://stackoverflow.com/questions/9011524/regex-to-check-whether-a-string-contains-only-numbers More on reddit.com
🌐 r/learnjava
21
5
January 23, 2020
Can anyone help me figure out how to check if an input is a number if the input is a String?
Just try the conversion, and if it's not valid, you get an exception. int number; try { number = Integer.parseInt (string); } catch (NumberFormatException nfe) { // do something since the string wasn't a parseable integer } More on reddit.com
🌐 r/java
10
2
December 18, 2013
How to check whether string is numeric in java without using exceptions?
Sorry, but this is one of the simplest things, absolute beginner level, with a naive, fail fast approach. All you need to do is to loop over the string character by character (hint .charAt) and check if the character under test is not inside the range '0' to '9' (conveniently, the characters are arranged from 0 to 9 in order in the ASCII and Unicode tables), so, a simple >= and <= will do the trick. If the character is not in the range, fail fast and return false, if the loop goes all the way through return true. This approach doesn't even have a bad runtime complexity. Worst case it is O(n) where n is the length of the string. It is fast and reliable. No exceptions, no regex. More on reddit.com
🌐 r/learnjava
21
5
January 29, 2025
check if number is binary.. and the number starts with a 0?

0111 is octal, i.e. base 8 not 10. (0111 = 73)

More on reddit.com
🌐 r/javahelp
3
7
August 22, 2017
🌐
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
In Java, there are several ways to check whether a String is numeric, including the following common approaches: Using the Integer.parseInt() or Double.parseDouble() methods ... You can use Integer.parseInt() to check whether a string is numeric ...
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java string › check if a string contains a number value in java
Check if a String Contains a Number Value in Java | Baeldung
May 11, 2024 - We can loop through the string and call isDigit() to check if the current character denotes a number:
🌐
GeeksforGeeks
geeksforgeeks.org › java › check-if-a-given-string-is-a-valid-number-integer-or-floating-point-in-java
Check if a given string is a valid number (Integer or Floating Point) in Java - GeeksforGeeks
July 23, 2025 - In the article Check if a given string is a valid number, we have discussed general approach to check whether a string is a valid number or not. In Java we can use Wrapper classes parse() methods along with try-catch blocks to check for a number.
🌐
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 ...
Find elsewhere
🌐
Coderanch
coderanch.com › t › 401142 › java › check-String-numeric
How to check if String() value is numeric [Solved] (Beginning Java forum at Coderanch)
October 13, 2005 - Even if it's not necessary to convert the value to a number, parseXxx() methods are convenient ways to check validity. Convenience is a good thing. And what if the programmer needs to identify a valid double? The logic here becomes more complicated - is it really necessary for the programmer ...
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-a-number
Java: Check if String is a Number
May 11, 2021 - Users tend to mistype input values fairly often, which is why developers have to hold their hand as much as possible during IO operations. The easiest way of checking if a String is a numeric or not is by using one of the following built-in Java methods: ... These methods convert a given String into its numeric equivalent. If they can't convert it, a NumberFormatException is thrown, indicating that the String wasn't numeric.
🌐
Quora
quora.com › How-do-you-check-if-a-string-is-an-integer-or-not-in-Java
How to check if a string is an integer or not in Java - Quora
Answer (1 of 7): To check if a string is an integer in Java, you can use several methods. One common approach is to use exception handling. Here's a sample code snippet that demonstrates how to do this: [code]public class CheckIfStringIsInteger { public static boolean isInteger(String str) {...
🌐
LabEx
labex.io › tutorials › java-checking-if-a-string-is-numeric-117415
Java Numeric String Parsing | Programming Tutorials | LabEx
The isCreatable() method is a simple and convenient method to check if a string is numeric. It also accepts numeric strings of hexadecimal numbers that start with 0x or oX, octal numbers that start with 0, scientific notations that use the letter ...
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-a-java-string-is-numeric-413946
How to check if a Java string is numeric | LabEx
When working with databases, you may need to check if a string value retrieved from the database represents a numeric type. This is particularly important when mapping database results to Java objects or when performing database queries. // Example database integration in a Java application try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE age > ?")) { String ageFilter = "25"; if (isNumeric(ageFilter)) { stmt.setInt(1, Integer.parseInt(ageFilter)); ResultSet rs = stmt.executeQuery(); // Process the query results } else { System.out.println("Invalid age filter: " + ageFilter); } } catch (SQLException e) { e.printStackTrace(); }
🌐
Quora
quora.com › How-can-you-tell-if-a-string-contains-numbers-in-Java
How to tell if a string contains numbers in Java - Quora
Being that every character is a ... and see for yourself), you can just check if your given character is in the range where all the numbers are (using plain old comparison operators)....
🌐
Coderanch
coderanch.com › t › 587407 › java › Checking-string-numbers
Checking if a string has numbers (Beginning Java forum at Coderanch)
Write down on a piece of paper how you intend to work out whether these Strings are integers or not: 123 0 000 -123 1234567890a number1 1.2 It is much easier if you confine yourself to decimal numbers. By the way, I think 000 is not a valid integer. ... As an alternative, consider (as Java does) that all integer literals are non‑negative, and reagrd a leading - as a sign‑change operator.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Check If String Is A Valid Number In Java - Java Code Geeks
January 3, 2025 - Here’s a breakdown of the code: The class Test contains a static method isValidNumber, which takes a string input and returns a boolean indicating whether the string is a valid number.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › Check-if-a-string-contains-a-number-using-Java
Check if a string contains a number using Java.
September 6, 2023 - JavaObject Oriented ProgrammingProgramming · To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class. Live Demo · public class ContainsExample { public static ...
🌐
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
The isCreatable() is a simple and convenient method that can be used to check if a string is numeric. It also accepts numeric strings of hexadecimal numbers that start with 0x or oX, octal numbers that start with 0, scientific notations that ...