Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better - Integer.parseInt() and catch exception or verify String with pattern before conversion? [duplicate]

Tags:

java

I have seen two styles for checking whether a variable is a valid integer in Java. One by doing an Integer.parseInt and catching any resulting exception. Another one is by using Pattern. Which of the following is better approach?

String countStr;
int count;
try {
    count = Integer.parseInt(countStr);
} catch (Exception e) {
    //return as the variable is not a proper integer.
    return;
}

or

String integerRegex = "([0-9]{0,9})";
if (countStr.isEmpty() || !Pattern.matches(integerRegex, countStr)) {
    //return as the variable is not a proper integer.
    return;
}

My question here is, is doing an Integer.parseInt() and catching an exception for validation a standard way to validate an int? I admit that my regex is not perfect. But is there any built-in methods available in Java for validation of int? Actually isn't it better to do some validation instead of simply catching the exception?

like image 876
afxgx Avatar asked Jun 13 '13 11:06

afxgx


People also ask

Does integer parseInt throw an exception?

We have used the parseInt method of the Integer class before. The method throws a NumberFormatException if the string it has been given cannot be parsed into an integer.

How do you check if a string can be converted to int?

Use the str. isdigit() method to check if a string can be converted to an integer, e.g. if my_str. isdigit(): . If the str.

What does parseInt return if not an integer?

If parseInt encounters a character that is not a numeral in the specified radix , it ignores it and all succeeding characters and returns the integer value parsed up to that point.


3 Answers

Using the approach above is better as it considers all types of possible errors and handles all cases. For instance what you have written will not parse negative numbers correctly.

It only makes sense to write your own verifier if you want to validate a given subset of all integers.

A general advice: don't re-invent the wheel unless you have strong reasons to do so.

like image 92
Ivaylo Strandjev Avatar answered Oct 11 '22 07:10

Ivaylo Strandjev


There's a really good reason to not go with the second approach if you actually want to check if the given string can be represented as a 32bit integer and not just that it represents an integer in the mathematical sense.

I'm sure we all agree that 2147483648 (2**31 for those paying attention) is a perfectly fine integer, but it's only one of infinitely many numbers for which the two options will give different results. So if you want to check if you can represent a string as a 32bit integer use the parseInt method, if you just want to see if it's an integer go with the regex.

PS: That said don't catch Exception, but the correct NumberFormat exception instead..

like image 42
Voo Avatar answered Oct 11 '22 08:10

Voo


These two function serve different purposes. If you just want to make sure that the string cotains a particular pattern, then use the second approach. If you need to convert it, then you should can parseInt() In this case it wouldn't make sense to check it and convert it as well.

However, if you have specific requirements for the number, then you may have to check it first regardless, because parseInt() may not always throw an exception if it can parse something which still doesn't fit your requirement.

like image 37
Devolus Avatar answered Oct 11 '22 07:10

Devolus