Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Char cannot be dereferenced!! Please help

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Char cannot be dereferenced!! Please help

    Hi all im working on a program and have got the error "char cannot be dereferenced". The for loop iterates through the array "people" and sends a message to each one using the method "getPerson()". This method gets a single character from a StringBuilder array. The method is declared as the return type char so i used .toString to try to change it into a string as I got a different error before. I am a novice in java so im not even sure im going about this theright way! I hope someone can help because I just cant see a way forward!

    Thankyou in advance for anyone willing to help.

     
     for (int i = 0; i < people.length; i++)
          {
             aHouse.getPerson();
             {
                if (aHouse.getPerson().toString() == "Y")  
                {
                   return "Yes";
                }
             }
          }
    Last edited by helloworld922; February 13th, 2010 at 03:07 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Char cannot be dereferenced!! Please help

    Strings should be compared using the .equals() method.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    humdinger (February 16th, 2010)

  4. #3
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Char cannot be dereferenced!! Please help

    I had already tried that before I posted but when I tried to compile it just kept telling me I needed a bracket on the line shown below and if I added a bracket it just continued giving that error:

    for (int i = 0; i < people.length; i++)
          {
             aHouse.getPerson();
             {
                if (aHouse.getPerson().toString().equals == "Y")  // compiler says i need an ) here
                {
                   return "Yes";
                }
             }
          }
    Last edited by helloworld922; February 15th, 2010 at 12:01 PM.

  5. #4
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Char cannot be dereferenced!! Please help

    it should be:
    aHouse.getPerson().equals("Y")

    if getPerson() returns a String you do not need to call toString().

    also your first call to aHouse.getPerson() seems pointless to me, as you do not bother to store the returning Object the second call to aHouse.getPerson() does exactly the same thing.

  6. The Following User Says Thank You to JavaDaveUK For This Useful Post:

    humdinger (February 16th, 2010)

  7. #5
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Char cannot be dereferenced!! Please help

    Hi thankyou for the advice, I have changed the code so its a you say but I get the same char cannot be dereferenced error when i try to compile.

  8. #6
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Char cannot be dereferenced!! Please help

    .toString() is an instance method for Objects. You say your getPerson() method is returning
    a char; a char is a primitive data type, not an Object! Just compare the returned value directly
    to the character 'Y' (note that inverted commas are used to denote character literals, and
    quotation marks for string literals).
    if(aHouse.getPerson() == 'Y')
    {
        //do stuff
    }

  9. The Following User Says Thank You to Shambolic For This Useful Post:

    humdinger (February 16th, 2010)

Similar Threads

  1. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM
  2. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM
  3. Need help with concatenizing char? to string?
    By bh-chobo in forum Java SE APIs
    Replies: 3
    Last Post: October 16th, 2009, 08:40 AM
  4. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM