Results 1 to 7 of 7
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default ERROR - char cannot be dereferenced HELP

    Hi guys,
    I am recieving a "char cannot be dereferenced" error when I try and compare to char's. This is only a section of the program. No other errors anywhere else.

    Thanks.

    Java Code:
    for (int i =0; i < list.size(); i++)
    		{
    			StringTokenizer st = new StringTokenizer(list.get(i), "|");
    			while(st.hasMoreTokens())
    			{
    				System.out.println("Would you like to see another line?");
    				String moreText = s.next().toLowerCase();//converts string to lower case
    				char c = moreText.charAt(0);
    				
    				while (c.equals("y"))
    				{
    					System.out.println(st.nextToken());
    				}
    				
    					break;
    				
    			}
    		}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Chars are compared with ==, only objects are compared with the equals method.

  3. #3
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Ok. Thats funny because the Character class does have a equals() Method. Thanks.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The Character class does have one, it's an object, the char is a primitive. The Character class is used as a wrapper class. If you wanted to create a generic bound list you need a proper class. It wont allow a primitive type. Through the wrapper class and aitoboxing you can declare a generically bound list of primitives.

    Java Code:
    ArrayList<Character> x = new ArrayList<Character>();
    Notice this uses Character, you can store chars in it though.
    Java Code:
    char z = 'g';
    x.add(z);

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by jonytek View Post
    Thats funny because the Character class does have a equals() Method.
    Because Character class is the wrapper class for char data type. The method equals belongs to Object class and in Java, "Object" is the superclass of every class, so Character class inherits the equals method from Object.

    I hope that has made it clear to you.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Character class inherits the equals method from Object.
    Um, Character overrides the equals(...) method inherited from Object.

    db

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by Darryl.Burke View Post
    Um, Character overrides the equals(...) method inherited from Object.
    Rightly so, to be more specific! :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. int cannot be dereferenced
    By jackrush in forum Advanced Java
    Replies: 2
    Last Post: 11-12-2010, 09:47 AM
  2. How can I solve "void cannot be dereferenced" error?
    By fervent07 in forum New To Java
    Replies: 2
    Last Post: 04-05-2010, 09:54 PM
  3. Long Cannot Be Dereferenced?
    By caps_lock in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 02:49 AM
  4. Replies: 2
    Last Post: 11-30-2008, 04:24 PM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •