Click to See Complete Forum and Search --> : Help with using .equals in arrrayList Extreme novice


jndonovan
March 22nd, 2010, 03:06 AM
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");
}
}

dlorde
March 22nd, 2010, 05:11 AM
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.

jndonovan
March 22nd, 2010, 06:00 AM
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.

dlorde
March 22nd, 2010, 06:27 AM
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

jndonovan
March 22nd, 2010, 06:37 AM
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