What's the best way to check if a String represents an integer in Java?

0 votes
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;
    }
    catch( Exception e ) {
        return false;
    }
}

Is it just me, or does this seem a bit hackish? What's a better way?
Apr 26, 2018 in Java by developer_1
• 3,220 points
82 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
If you are not concerned with potential overflow problems this function will perform about 20-30 times faster than using Integer.parseInt().

public static boolean isInteger(String str) {
    if (str == null) {
        return false;
    }
    int length = str.length();
    if (length == 0) {
        return false;
    }
    int i = 0;
    if (str.charAt(0) == '-') {
        if (length == 1) {
            return false;
        }
        i = 1;
    }
    for (; i < length; i++) {
        char c = str.charAt(i);
        if (c < '0' || c > '9') {
            return false;
        }
    }
    return true;
}
answered Apr 26, 2018 by Rishabh
• 3,520 points
0 votes

You can also use regular expression.

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

It will give you output as:

-?     --> negative sign, could have none or one
\\d+   --> one or more digits
answered Aug 9, 2018 by Daisy
• 8,020 points

Related Questions In Java

0 votes
1 answer

what is the best way to convert an ArrayList to a String

String lString = String.join(", ", list); In case ...READ MORE

answered Apr 26, 2018 in Java by developer_1
• 3,220 points
31 views
0 votes
1 answer

What is the easiest way to iterate through the characters of a string in Java?

The below code seems the easiest to ...READ MORE

answered Jul 13, 2018 in Java by scarlett
• 1,280 points
27 views
0 votes
1 answer

In Java, what is the best way to determine the size of an object?

Please check the below code to arrive ...READ MORE

answered Nov 21, 2018 in Java by Frankie
• 9,570 points
48 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,020 points
441 views
0 votes
1 answer

The best way to filter a Java Collection?

 this problem is solved using streams and ...READ MORE

answered May 28, 2018 in Java by parth295
• 4,590 points
44 views
0 votes
1 answer

How to pad an integer with zeros on the left in Java?

Use java.lang.String.format() method. String.format("%05d", number) ...READ MORE

answered May 31, 2018 in Java by Daisy
• 8,020 points
43 views
0 votes
1 answer

What is the simplest way to read JSON from a URL in java

Read json from url use url.openStream() and read contents ...READ MORE

answered Jun 13, 2018 in Java by samarth295
• 2,190 points
86 views
0 votes
1 answer

What is an efficient way to implement a singleton pattern in Java?

Use an enum: public enum Foo { ...READ MORE

answered Jul 6, 2018 in Java by Akrati
• 3,150 points
54 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
47 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,520 points
32 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.