import cs1.Keyboard;
public class charAT {
public static void main(String[] args) {
String s;
//The telephone number should be 79575757
System.out.println("Enter your telephone number");
s = Keyboard.readString();
int index1 = s.charAt(0);
This part does not work:
if((char)index1 == 7){
System.out.println("The second number is "+(char)index1);
}
else {
System.out.println("This number is invalid");
}
}
}
What am I doing wrong?
First, charAt() method returns
char, so your code should be like this:I changed the variable name from
index1toch. charAt() method returns thecharat the specified position, not an index.Second, you should compare the
char:And the last, you can concatenate
Stringandcharlike this: