• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

charAt(0)

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone tell you you're wonderful. Thank you!
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
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
I don't think autoboxing does this kind of transformation ( x.m() -> Wrapper.m(x) ). It would be a little much to ask.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic