View Full Version : help with one method in java

Mar 19, 2007, 10:02 PM
my method is short an nearly complete, i just need to check to see if that element in the array is occupied.
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.


Mar 19, 2007, 10:21 PM
I guess it sort of depends on what constitutes "occupied".

How about something like this:

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


Mar 19, 2007, 10:56 PM
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

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
public boolean equals (Object obj) {
try {
if (compareTo(obj) == 0)
return true;
}catch(Exception e)
return false;

Mar 19, 2007, 11:12 PM
nevermind figured it out, dont worry about it:D