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 cannot be dereferenced
Code:
do { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter String"); String raw = br.readLine(); if (raw.length() < 10) { System.out.println(""); System.out.println("Please input a valid phone number of at least 10 digits/letters"); System.out.println(""); } else { if (raw.charAt(3).equals('-') && raw.charAt(7).equals('-')) { System.out.println("2 Hyphens at 3 and 7"); } else if (raw.charAt(3).equals('-') && raw.charAt(8).equals('-')) { System.out.println("2 Hyphens at 3 and 8"); } else if (raw.charAt(3).equals('-') && raw.charAt(9).equals('-')) { System.out.println("2 Hyphens at 3 and 9"); } } } while (1 < 2);
The type char is a primitive -- not an object -- so it cannot be dereferenced. Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.
The charAt() method in Java returns the char value of a character in a string at a given or specified index.
equals() is a method of all Java Objects. But char is not an Object type in Java, it is a primitive type, it does not have any method or properties, so to check equality they can just use the == equals operator.
The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
The above code will throw the error “char cannot be dereferenced” because we’re trying to call equals method on a char data type variable. Which doesn’t exist. Instead of calling equals method, we can compare a character using “==”.
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 !=.
Need some help about ".charAt" and ".equals" just a newbie. A char is a primitive; a Character (or indeed a String) is an object, and therefore a reference type. You use '==' with primitives and .equals () with objects. String.charAt () returns a char, so the message was telling you that .equals () is not appropriate.
In Java, reference is an address to a variable or object, and dereferencing means to access the features of a variable or object through that reference. Char is a primitive type variable, and dereferencing a primitive type will throw the error char cannot be dereferenced or cannot invoke a method on primitive type char.
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 !=
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With