• Post Reply Bookmark Topic Watch Topic
  • New Topic

charAt(0)  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reg is a String

I have the following code:



I am getting the following error when compiling:
C:\java\PoliceDatabase>javac TestApp.java
.\VehicleRecord.java:419: char cannot be dereferenced
if (reg.charAt(0).isLetter( ) )
^
1 error
And I don't understand why? Please could someone explain it to me.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charAt() returns a char, a primitive. You can't call a method on a primitive. Writing something.anythingElse in Java is called "dereferencing" something, and you can't dereference a primitive.

The isLetter() method you're thinking about is a static method in the Character class that takes a char as an argument -- i.e.,

if (Character.isLetter(reg.charAt(0))) ...

Remember, a char is not a Character, an int is not an Integer, a float is not a Float, etc.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone tell you you're wonderful. Thank you!
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

Remember, a char is not a Character, an int is not an Integer, a float is not a Float, etc.


Have anyone tried this with Java 5.0. I am wondering if the boxing & unboxing feature will take care of this.

Just wondering, as I just switched back to JDK 1.4 for an assignment.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think autoboxing does this kind of transformation ( x.m() -> Wrapper.m(x) ). It would be a little much to ask.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!