If input value can be in numeric form other than integer , check by

if (x == (int)x)
{
   // Number is integer
}

If string value is being passed , use Integer.parseInt(string_var). Please ensure error handling using try catch in case conversion fails.

Answer from Mudassir Hasan on Stack Overflow
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-check-if-input-is-integer-117391
Check if Input Is Integer in Java | LabEx
Learn three methods to check if the given input is an integer in Java, including using Integer.parseInt(), Scanner.hasNextInt(), and Character.isDigit().
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ business courses โ€บ business 104: information systems and computer applications
How to Check If a String is an Integer in Java - Lesson | Study.com
March 9, 2018 - To recap, strings are powerful ... if a string is an integer, we can use the parseInt function to convert the string to an integer, and pass back a true value if it succeeds....
๐ŸŒ
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) {...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] is there a way to easily check if a given value is a whole number or not?
r/learnprogramming on Reddit: [Java] Is there a way to easily check if a given value is a whole number or not?
January 18, 2016 - I agree with what u/Sorten said. Another alternative, if you're using a Scanner to get the input, is using an InputMismatchException. That allows you to check for an integer(which is a whole number by definition).
๐ŸŒ
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 Java we can use Wrapper classes parse() methods along with try-catch blocks to check for a number. ... Integer class provides a static method parseInt() which will throw NumberFormatException if the String does not contain a parsable int.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ check if input is integer in java
How to Check if Input Is Integer in Java | Delft Stack
February 2, 2024 - The Scanner class is an essential utility in Java that allows for tokenization and parsing of input. It can break the input into tokens based on specified delimiters, making it easier to handle various types of input. The Scanner class offers a convenient method named hasNextInt() that allows you to check whether the next token in the input stream is an integer. It returns true if the next token is an integer and false otherwise.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-you-check-if-a-value-in-Java-is-of-an-integer-type
How to check if a value in Java is of an integer type - Quora
Answer (1 of 8): The best way is to use a try catch block... If you are getting any value in String or in Object type variable, you can simply parse it using Integer.parseInt() and assign it to some int variable. If it is integer in nature, it will work otherwise NumberFormatException will get ge...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 606406 โ€บ java โ€บ check-integer-java
how to check if a value is integer in java (Beginning Java forum at Coderanch)
If the entered value is an integer, than I let the user proceed; However, If it isn't the user then the code should return a false string. can anyone help me with a java code for it? ... Can you paste your code and explain what exactly is it that you are having a problem with? Use code tags. ... have you checked the API to see if there is something that could help?
๐ŸŒ
W3Docs
w3docs.com โ€บ java
How can I check if a value is of type Integer?
To check if a value is of type Integer in Java, you can use the instanceof operator.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-check-if-a-number-is-an-integer-in-java-559960
How to Check If a Number Is an Integer in Java | LabEx
Learn how to check if a number is an integer in Java using Math.floor(), test with double and float, and handle string input conversion. Practical skills for validating numerical data.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ check if an integer value is null or zero in java
Check if an Integer Value Is Null or Zero in Java | Baeldung
January 8, 2024 - Using the logical OR operator could be the first idea to perform the check. It simply checks if the given Integer number is null or zero.
Top answer
1 of 9
364

The most naive way would be to iterate over the String and make sure all the elements are valid digits for the given radix. This is about as efficient as it could possibly get, since you must look at each element at least once. I suppose we could micro-optimize it based on the radix, but for all intents and purposes this is as good as you can expect to get.

public static boolean isInteger(String s) {
    return isInteger(s,10);
}

public static boolean isInteger(String s, int radix) {
    if(s.isEmpty()) return false;
    for(int i = 0; i < s.length(); i++) {
        if(i == 0 && s.charAt(i) == '-') {
            if(s.length() == 1) return false;
            else continue;
        }
        if(Character.digit(s.charAt(i),radix) < 0) return false;
    }
    return true;
}

Alternatively, you can rely on the Java library to have this. It's not exception based, and will catch just about every error condition you can think of. It will be a little more expensive (you have to create a Scanner object, which in a critically-tight loop you don't want to do. But it generally shouldn't be too much more expensive, so for day-to-day operations it should be pretty reliable.

public static boolean isInteger(String s, int radix) {
    Scanner sc = new Scanner(s.trim());
    if(!sc.hasNextInt(radix)) return false;
    // we know it starts with a valid int, now make sure
    // there's nothing left!
    sc.nextInt(radix);
    return !sc.hasNext();
}

If best practices don't matter to you, or you want to troll the guy who does your code reviews, try this on for size:

public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    } catch(NullPointerException e) {
        return false;
    }
    // only got here if we didn't return false
    return true;
}
2 of 9
316

It's better to use regular expression like this:

str.matches("-?\\d+");

-?     --> negative sign, could have none or one
\\d+   --> one or more digits

It is not good to use NumberFormatException here if you can use if-statement instead.

If you don't want leading zero's, you can just use the regular expression as follow:

str.matches("-?(0|[1-9]\\d*)");
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 237159 โ€บ whats-the-best-way-to-check-if-a-string-represents-an-integer-in-java โ€บ 23630046
What's the best way to check if a String represents an integer in Java? - Stack Overflow
I normally use the following idiom to check if a String can be converted to an integer. public boolean isInteger( String input ) { try { Integer.parseInt( input ); return true;...
๐ŸŒ
Studytonight
studytonight.com โ€บ java-examples โ€บ check-if-input-is-integer-in-java
Check if Input is Integer in Java - Studytonight
The Scanner.hasNextInt() method checks whether the current input contains an integer or not. If the integer occurred in input this method will return true otherwise it will return false. import java.util.Scanner; public class StudyTonight { ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 641970 โ€บ java โ€บ Validate-Integer
How to Validate and Integer from 0 - 9? (Beginning Java forum at Coderanch)
Actually if you write 0-9 the compiler will see that as the expression ... One possibility: You can see whether the int is within a specific range. You need to check for both ends of the range and conjoin both tests with the AND operator. Another possibility: Use a regular expression to check whether the text in the text box matches a single digit. The Java Tutorials has a good section about regular expressions.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ program-check-input-integer-string
Program to check if input is an integer or a string - GeeksforGeeks
To check if an input is an integer or a string using the Integer.equals() method in Java, you can follow these steps:
Published ย  May 24, 2024
๐ŸŒ
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