Return true if code contains at least one digit, and false if it does not.
hasDigit("abc0") returns true. hasDigit("abc") returns false.
10 strips: G1 to G10
for (int i = 0; i < code.length(); i++) { G1return true; G2if ("0123456789".indexOf(ch) > 0) { G3return false; G4if ("0123456789".indexOf(ch) != -1) { G5} G6public static boolean hasDigit(String code) { G7} G8} G9String ch = code.substring(i, i + 1); G10Order: G7, G1, G10, G5, G2, G9, G6, G4, G8. Extra: G3
public static boolean hasDigit(String code) {
for (int i = 0; i < code.length(); i++) {
String ch = code.substring(i, i + 1);
if ("0123456789".indexOf(ch) != -1) {
return true;
}
}
return false;
} Extra strip: if ("0123456789".indexOf(ch) > 0) {. indexOf returns 0 for "0", so > 0 treats the digit 0 as not found. Not found is -1.