Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
my method is short an nearly complete, i just need to check to see if that element in the array is occupied.
Code:
 public void insertSong (int track, String artist, String title, int length){
        
        Song newSong = new Song(length, artist, title);
           songs[track - 1] = newSong;

     }

i had one method like this that was pretty easy, but its parameter wa an array of song objects, but i have to have this one to override it. it's for school.

thanks
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
I guess it sort of depends on what constitutes "occupied".

How about something like this:
Code:
if (songs[track - 1] instanceof Song) { 
    // A song already exists in that slot 
}
else { 
    // The array element does not contain a Song, but might contain something else 
}

Todd
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
thanks, theres one more thing i need help with. my findSong() method needs to use this equals() method thats in another class, i figured out how to do this a different way(without equals()).
heres what i figured out, next is the equals method
Code:
        int track = 0;

        for (int i = 0; i < songs.length; i++) {
            if (songs[i].getArtistName() == artist &&
                songs[i].getSongName() == title)
                track = i + 1;
            //else track = -1;
        }
        if(track == 0){
            System.out.print("This song or the artist is not on the album. result:");
            track = -1;
        }
        if (track > 0)
            System.out.print("The track number from the artist and song title" +
                            " you provided is track ");

        return track;
heres the equals method, which uses the compareTo interface
Code:
public boolean equals (Object obj) {
        try {
             if (compareTo(obj) == 0)
                 return true;
        }catch(Exception e)
        {
        }
        return false;
        }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.