Doing some exercises for Java, and I'm trying to verify social security (specific for my country)
Something doesn't add up for me with charAt(0) Given social security "300198", it prints "Error 1" at the moment.
public void authenticateCpr(Member member){
if (member.getCpr().charAt(0) > 3) {
System.out.println("Error 1");
}
if (member.getCpr().charAt(0) == 3 && member.getCpr().charAt(1) > 9){
System.out.println("Error 2");
}
}
What am I missing?
String.charAt(0)
returns '3'
which ASCII code equals to 51
. You should write something like this
public void authenticateCpr(Member member){
if (member.getCpr().charAt(0) > '3') {
System.out.println("Error 1");
}
if (member.getCpr().charAt(0) == '3' && member.getCpr().charAt(1) > '9'){
System.out.println("Error 2");
}
}