This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to compare character to comma Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to compare character to comma" Watch "how to compare character to comma" New topic
Author

how to compare character to comma

Michael Waserman
Greenhorn

Joined: Jul 10, 2004
Posts: 28
I am trying to compare first character in line to comma and getting "char cannot be dereferenced" error. Here is my code:

line.charAt(0).toString().equals(",");


What am i doing wrong?
Thanks. michael.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

To "dereference" means (in so many words) to do anything that involves putting a dot right after the thing -- i.e., calling a method with thing.something(), or trying to read a member variable, with thing.something else. You're trying to call a method toString() on a char (the return value of charAt() ) and you can't do that. You can't dereference any of the primitive types: byte, short, char, int, long, float, double, or boolean.

To compare characters (or any other primitives), you can use the == operator rather than the equals() method.

line.charAt(0) == ','

Note the single quotes: that's a character constant, not a String.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to compare character to comma
 
Similar Threads
Conditional 'where' condition
Compare operators
umlauts
Question About Regex Chapter-6 K&B
"char cannot be dereferenced error"