1

I'm trying to do a foreach loop with an ArrayList... here's the story...

I have an ArrayList:

     ArrayList<Album> coll = new ArrayList<Album>();

This contains information about a number of albums that make up a collection/library...

The Album class contains a method that returns the album name in string.

I'm basically trying to find out if album already exists or not with a foreach loop.

I have this method:

    public Boolean findAlbumByName(ArrayList<Album> albumList, String name){
        for (Album album : albumList)
        {
            if (album.getName().equals(name))
            {
                return true;
            }
        }
        return false;
    }

The problem occurs when I try to do this statement:

      if(findAlbumByName(coll, 'example song') == false)
      {
         // code here
      } 

It has an error that reads: The method findAlbumByName(ArrayList<Album>, String) is undefined for the type Album.

Any help or clue would be highly appreciated. Thank you.

5
  • coll must be an instance of ArrayList<Album> and not an instance of Album. Commented Aug 23, 2011 at 19:44
  • 1
    First make sure your code compiles. It can't compile as is (String literals must be enclosed in double quotes). If using Eclipse, look at the Problems view and make sure there is no error. Commented Aug 23, 2011 at 19:48
  • Also you may want to use List<Album> instead of ArrayList<Album> Commented Aug 23, 2011 at 19:49
  • I don't see how the code as explained can cause "undefined method" error. Commented Aug 23, 2011 at 19:52
  • Where is your second statement ? Is it in the same Album class? Commented Aug 23, 2011 at 19:53

5 Answers 5

4
  1. You need to change 'example song' to "example song"

  2. And you may want to change

    public Boolean findAlbumByName
    

    to

    public boolean findAlbumByName
           ^
    
  3. Another way of writing condition == false is to negate the condition, like !condition.


Here is an example implementation if you're still stuck.

Sign up to request clarification or add additional context in comments.

4 Comments

With auto-boxing, there's no real reason to return a primitive.
There's no reason to return a boxed type either really. (Except that clients may unneccesarily worry about null return values.)
The 'example song' issue causes a Invalid character constant error and not method is undefined. That change alone wouldn't change anything, it seems to me.
@aioobe, thank you very much man! both answers were really helpful.
2

The method findAlbumByName(ArrayList<Album>, String) is undefined for the type Album means that you are trying to call the method from the Album class, even though you have not defined the method on that class. To get your code to work, you need to do two things:

  1. Move the method into the Album class (if that's where it belongs)
  2. Change your string so that it uses double-quotes (") rather than single quotes (').

Once you do that, your method should work just fine.

1 Comment

yes, thank you. this is also a helpful answer and i will consider. thank you once again...
0

Maybe with the full code it would be easier.. By the way, given that your function implements a use-case (i.e. it's a general function that it's not called on any instance of the class Album in particular), shouldn't it be static??

`public static boolean findAlbumByName(ArrayList albumList, String name);

Also, have you considered using the boolean contains(Object elem) of ArrayList? Remember to override the boolean equals(Object o)' inAlmbum` though!!

Comments

0
public Boolean findAlbumByName(ArrayList<Album> albumList, String name)

should be (notice lower case boolean)

public boolean findAlbumByName(ArrayList<Album> albumList, String name)

and call it this way (notice qualifying a string with double quotes - single quote is for char types):-

if(!findAlbumByName(coll, "example song"))

Also you should consider using an interface on the left hand side in your ArrayList declaration. Example:

List<Album> coll = new ArrayList<Album>();

3 Comments

Why comparing it to false? Just use if (!findAlbumByName(coll, "example song"))
@CoolBeans, thank you very much, your answer was really helpful. I'm not sure if its the lower case or the double quotations but something has worked. You're a STAR!!
@JB Nizet - I just tried to fix the compile issues the OP had. Obviously, using ! is more readable. I didn't want to get the OP confused by changing his syntax since it seems he is new to development.
-2
 if ( ! findAlbumByName(coll, 'example song') )
  {


  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.