Reputation: 771
I was trying to write a piece of code to test if a string contains an integer. I am aware of the try catch solution, but i have read it becomes bad if you call the method from other classes. What I have read is that the method will show the error, but the main of the class calling it will keep running anyway. Therefore, I was trying to do it manually. My problem is that i am able to assess that the string is not empty and that all the characters in the string are digits, but I can't find a way to verify whether the number is too big to be sorted in an integer. Point is, i have found on stackoverflow many similar topics, but noone solve this problem without try catch. Here is my method.
// INTEGER VERIFICATION
public static boolean isInteger (String str_input){
int number_of_digits = 0;
if (str_input.isEmpty()) {
JOptionPane.showMessageDialog(null, "No input inserted", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
for (char c : str_input.toCharArray()){
if(Character.isDigit(c)){
number_of_digits++;
}
}
if (number_of_digits == str_input.length()){
return true;
}
else {
JOptionPane.showMessageDialog(null, "The input is not an integer", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
Thanks in advance for your help!
Upvotes: 0
Views: 2422
Reputation: 6739
How about using Regex
-?\\d+(\\.\\d+)? which accept negative and decimal numbers
Source: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
Upvotes: 0
Reputation: 25873
I think the best way to do this is as pointed by Leo in the comments.
public static boolean isInteger(final String strInput) {
boolean ret = true;
try {
Integer.parseInt(strInput);
} catch (final NumberFormatException e) {
ret = false;
}
return ret;
}
Also I suggest you separate the GUI part from the checking method, let the caller decide what to do if false (for example, maybe in some situations you want to check if it's an integer but don't show the dialog).
Upvotes: 2
Reputation: 393831
You could modify your method to make sure the number fits within an int.
It can be done by parsing the input as long, and checking against the range of int numbers.
// INTEGER VERIFICATION
public static boolean isInteger (String str_input){
int number_of_digits = 0;
if (str_input.isEmpty()) {
JOptionPane.showMessageDialog(null, "No input inserted", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
for (char c : str_input.toCharArray()){
if(Character.isDigit(c)){
number_of_digits++;
}
}
if (number_of_digits == str_input.length()){
if (str_input.length > 15) // arbitrary length that is too long for int, but not too long for long
return false;
long number = Long.parseLong(str_input);
if (number > Integer.MAX_VALUE || number < Integer.MIN_VALUE)
return false;
else
return true;
}
else {
JOptionPane.showMessageDialog(null, "The input is not an integer", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
BTW, if you allow negative inputs, you should change your check to allow '-' as the first character.
That said, I agree with all the comments that say you'd be better off to just call Integer.parseInt() and catch the exception.
Upvotes: 0