Tried both in my IDE and in the online IDE my textbook on Zybooks.com gives me to work in. My goal is to check whether or not the variable passCode
contains a digit. Here's the code:
public class CheckingPasscodes{
public static void main (String [] args) {
boolean hasDigit = false;
String passCode = "";
int valid = 0;
passCode = "abc";
if (passCode.isDigit(passCode.charAt(0)) || passCode.isDigit(passCode.charAt(1)) || passCode.isDigit(passCode.charAt(2))) {
hasDigit = true;
}
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
}
}
I get the error on line 9 (where the first if
starts):
java: cannot find symbol
symbol: method isDigit(char)
location: variable passCode of type java.lang.String
Tried changing passCode.charAt(0)
(or 1 and 2) to simply 'a'
(or 'b' and 'c') to test whether it was because I was putting a method inside another method, but I seem to get the same error.
I eventually solved the problem by asking a friend, who provided me this, instead:
char s = passCode.charAt(0);
char s1 = passCode.charAt(1);
char s2 = passCode.charAt(2);
if ((s>'0' && s<'9') || (s1>'0' && s1<'9') || (s2>'0' && s2<'9')) {
hasDigit=true;
}
It makes perfect sense, and I did think of doing something similar, but the chapter in which this exercise is isn't about .charAt()
—we covered that before—but rather about .isDigit()
and other character operations, so doing what I did is all but cheating.
This is driving me nuts.
P.S.: I'm pretty new to Java, so, if the answer is something really obvious, I'm really sorry. And thanks for taking your time to read all this!