Reputation: 87
Instructions: Given a string, determine if it is an integer. For example the string “123” is an integer, but the string “hello” is not.
It is an integer if all of the characters in the string are digits.
Return true if it is an integer, or false if it is not.
Hint: There is a method Character.isDigit() that takes a char as an argument and returns a boolean value.
What I have so Far:
public boolean isInteger(String str) {
if(Character.isDigit(str.charAt(0)) == 0) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if(Character.isDigit(str.charAt(i))) {
break;
} else {
return false;
}
}
return true;
}
I'm having an issue with returning a boolean value for the string "101" and no string at all (" ")
Upvotes: 1
Views: 30708
Reputation: 191701
You could use a regular expression.
return str.matches("\\d+");
won't work for negative numbers, though.
You could also use Integer.parseInt
and catch the NumberFormatException
and return true/false accordingly.
Or, you should not break the first digit you find, as you need to check all characters, only until you find one that is not a digit. Again, this does not capture negative numbers
public boolean isInteger(String str) {
if(str == null || str.trim().isEmpty()) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if(!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
Personally, option 2 is the best, but your instructions seem to imply that you need to iterate over character values
Upvotes: 8
Reputation: 1229
All the above solution seems correct to me but I would like to give one more solution to this problem. You can check this using ASCII values too:
private Boolean IsInteger(String str)
{
int length = str.length(),c=0;
if(length==0)
return false;
for(int i=0;i<length; i++)
{
c = (int)str.charAt(i)-48;
if(!(c>=0 && c<10))
return false;
}
return true;
}
I think My solution is much better because it used less number of library functions per iterations.
I am not checking for negative values because in the questions you have specified that the string is Integer if all the characters are digits. If you want to check negative numbers you can do it by adding one condition in i==0;.
Upvotes: 2
Reputation: 366
In java, you can use this regex pattern : It will filter either negative or positive value
private static final String NUMBER_REGEX = "(^\\d++)|(^[\\-]{1}\\d++)";
private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_REGEX);
final Matcher matcher = NUMBER_PATTERN.matcher(inputString);
if(matcher.find()) {
// If matcher => parse the value
final String sNumber = matcher.group();
final int value = Integer.parseInt(sNumber);
} else {
// Not matching
}
Upvotes: 0
Reputation: 1192
You are close, but have mixed up logic. I'll go through it with you now.
if(Character.isDigit(str.charAt(0)) == 0) {
return false;
}
I'm not sure what this above statement if trying to test. I see that you are checking if the After reading your comment it seems that it was meant to check for 0
index is a digit, but then you compare it to 0
. Even if this was written correctly, this statement can be removed because you are about to iterate through the string.null
or empty string. In which case you may want to test the string
directly.
if(str == null || str.isEmpty())
return false;
for (int i = 0; i < str.length(); i++) {
if(Character.isDigit(str.charAt(i))) {
break;
}
else {
return false;
}
}
return true;
The above break;
does not do what I think you think it does. break;
will exit out of the for
loop, thus only checking the first character
. I suggest rewriting this if-else
to get rid of the else
and negate your isDigit
method. See my code below.
for(int i = 0;i < str.length();i++){
if(!Character.isDigit(str.charAt(i))
return false;
}
return true;
You only want to return false
if the character
is not a digit.
Upvotes: 2
Reputation: 11
I suggest using regex to identify integer.
Like this
public boolean isInteger(String str){
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(str);
boolean b = m.matches();
return b;
}
Upvotes: 1
Reputation: 838
Just check if Integer.parseInt throws exception or not.
try {
Integer.parseInt(string);
} catch (NumberFormatException e) {
return false;
}
return true;
Upvotes: 3