• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to compare character to comma

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic