I am trying to check a string for hyphens at different positions (for a phone number because the input varies), but I keep getting the error
char can
If you use something like this, it will work:
if (raw.charAt(3) == '-' && raw.charAt(7) == '-') {
System.out.println("2 Hyphens at 3 and 7");
} else if (raw.charAt(3) == '-' && raw.charAt(8) == '-') {
System.out.println("2 Hyphens at 3 and 8");
} else if (raw.charAt(3) == '-' && raw.charAt(9) == '-') {
System.out.println("2 Hyphens at 3 and 9");
}
The problem is that raw.charAt(n)
returns a char
and not a String
. The equals()
method can be used only on objects. Char is a primitive data type which has no methods. On chars you should use operators like ==
or !=
.