Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dereferenced??

 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i am trying to allow a user to enter 3 characters from a menu A, S, and L. the program works fine. but my question is when i tried to place if (ch.equalsIgnorCase('A'))

{//diplsay message from selecting 'A' from menu.
System.out.println("Add a person");
}
to the program so that it didnt matter if the y used upper case or not i get the error message:
lowChartA.java [24:1] char cannot be dereferenced
if (ch.equalsIgnorCase('A'))
^
not sure what to do here.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Primitive" types, like byte, char, short, int, long, float, double, and boolean, don't have members or methods and so can't be "dereferenced". (To dereference X just means to write X.something .) You can use the various built-in operators to operate on chars, though. To see if a character is an upper or lower-case A, you would write

if (ch == 'a' || ch == 'A') { ...
 
Garry Meax
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope this help you.

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equalsIgnoreCase() method can only be used on Strings, not on chars. To compare chars, you follow EFH's suggestions above. One nice thing about chars is that you can use them in a switch statement, but you cannot use switch with Strings. For example:


Layne
[ February 23, 2005: Message edited by: Layne Lund ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic