CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    3

    Help with using .equals in arrrayList Extreme novice

    Hi there I am just trying to learn java and am really stuck on a problem, was just wondering if anyone can help me figure it out. I may not be able to explain what i am trying to do well enough for anyone to understand so don't be too hard on me

    I have 2 classes and am using an arrayList to add objects from class Track into class PlayList which contains the arrayList. I am trying to use the .equals method of the string class to find an artist by name and print out their details if their name is searched for and found. I have written the following code and get the error message ')' expected but don't understand what that means?


    public void String (Track returnName)
    {

    if (returnName.equals(Track artist))

    {
    System.out.println(printDetails);
    }
    else

    {
    System.out.println("There is no artist with this name");
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help with using .equals in arrrayList Extreme novice

    The call to 'returnName' has confused the compiler by using 2 parameters ('Track' and 'artist') instead of one. The class name 'Track' should not be used when calling the method. Class names are used in method parameter declaration, not in method calling.

    If you understand what you're doing, you're not learning anything...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Mar 2010
    Posts
    3

    Re: Help with using .equals in arrrayList Extreme novice

    Thankyou very much!
    I have changed it to just artist and now it tells me "cannot find variable artist"? artist being the field in class Track that I want to link to and search for the artist by name and print details if the statement returns true.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help with using .equals in arrrayList Extreme novice

    If you get an error, please post the full text of the error message and the relevant code.

    If 'returnName' is the Track that contains the artist you want to use, you can probably access it by using 'returnName.artist'.

    You haven't posted the Track code, so I'm guessing, but Best Practice generally recommends making all fields 'private' and providing getXxxx() methods to access them, rather than accessing them directly. How a class stores its data is nobody's business but its own...

    I would also recommend using meaningful names - calling a method 'String' is a very bad idea - String is a class name; and calling a Track object 'returnName' is opaque to say the least...

    BTW - by the Java conventions, method and variable names start with a lowercase letter and class names start with an uppercase letter.

    The first step towards wisdom is calling things by their right names...
    Chinese proverb
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Mar 2010
    Posts
    3

    Re: Help with using .equals in arrrayList Extreme novice

    Thanks heaps for your time I have found your comments very helpful and think I can figure it out
    Also thanks for the advice I shall take that on board

    Much appreciated

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured