Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

charAt().equals() causes "char cannot be dereferenced"

Tags:

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); 
like image 577
Ds.109 Avatar asked Jan 11 '14 21:01

Ds.109


People also ask

What does char Cannot be Dereferenced mean?

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.

Does charAt return char or string?

The charAt() method in Java returns the char value of a character in a string at a given or specified index.

How do you check if a char is equal to another char in Java?

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.

What does charAt () method returns?

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.

Why Char cannot be dereferenced in C++?

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 “==”.

Why can't I use the equals () method on a char?

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 !=.

What is the difference between 'Charat' and 'equals'?

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.

What does it mean to dereference a char in Java?

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.


1 Answers

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 !=.

like image 153
N Alex Avatar answered Sep 30 '22 19:09

N Alex