I would like to find whether a string contains more than one number. I have two different statement, the first one which I wrote myself does what I want, but the second does not work. I am trying to figure the meaning of the second statement.
There is A:
private static boolean moreThan2Num(String string) {
int numberOfNumbers = 0;
for (int i = 0; i < string.length(); i++) {
if (Character.isDigit(string.charAt(i))) {
numberOfNumbers++;
if (numberOfNumbers > 1) {
return true;
}
}
}
return false;
}
Here is B:
static boolean moreThan2Num(String str) {
for(int i=0;i<str.length();i++) {
if(!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
Here is my main:
public static void main(String[] args) {
String str1 = "as3as123aaag3";
String str2 = "qweqweqwe";
System.out.println(String.format("more then 4 number in \"%s\" - %s", str1, moreThan2Num(str1)));
System.out.println(String.format("more then 4 number in \"%s\" - %s", str2, moreThan2Num(str2)));
System.out.println(String.format("more then 4 number in \"%s\" - %s", str1, moreThan2Num1(str1)));
System.out.println(String.format("more then 4 number in \"%s\" - %s", str2, moreThan2Num1(str2)));
}
I thought the second one should work, but it did not.