Okay so my loop is supposed to check if an item in the ArrayList has the MINIMUM_RATING value which is 1. It does that just fine, but the problem starts when there is more than one item with the MINIMUM_RATING value in it. I need it to stop at the first item with that value in it. Here is the method I wrote.

public int getFirstIndexWithMinRating() {
        String minName = "";
        for(Track nextTrack : tracks) {
            if (nextTrack.getRating() == Track.MINIMUM_RATING)
            minName = nextTrack.getName();
        }
        return indexOf(minName);
    }

and here is the test method that I wrote that showed me the problem.

public void testShouldReturnOneWhenSecondAndLastSongHasMinRating() {
		theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 2));
		theTracks.add(new Track("song 2", 1));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 1));

		assertEquals(1, theTracks.getFirstIndexWithMinRating())

The value returned is 3 and I need it stop at song 1. Any help would be greatly appreciated.

Try to use this:

public int getFirstIndexWithMinRating() {
        String minName = "";
        for(Track nextTrack : tracks) {
            if (nextTrack.getRating() == Track.MINIMUM_RATING)
            minName = nextTrack.getName();
break;
        }
        return indexOf(minName);
    }

Hope it helps.
Or simply

without loop
return indexOf(Track.MINIMUM_RATING);

sory the second proposition is wrong I miss it sorry

The thing is though I am really trying to not use break. There is a certain hatred around here of that function and it really can make things tough because of it.

Moutanna's suggestion was a good one; break in this case would be a good idea. If you don't want to use break, though, you can use a for loop and make one of the conditions a boolean. Set that boolean to true once you find the value so it will end the loop.

for (int i = 0; i < size && !finished; i++){
   if (youFindTheMinimumRating) finished = true;
}

There are a ton of other ways to do this..

Okay so I tried the break function, but only if the min value is the first in the ArrayList it will return the right value. Otherwise it will return negative one. Here is my code.

}
    
    public int getFirstIndexWithMinRating() {
        String minName = "";
        for(Track nextTrack : tracks) {
            if (nextTrack.getRating() == Track.MINIMUM_RATING)
            minName = nextTrack.getName();
            break;
        }
        return indexOf(minName);
    }

Here are my test classes.

public class WhenReturnIndexOfFirstTrackWithMinRating extends junit.framework.TestCase
{
	private Playlist theTracks;

	public void testShouldReturnZeroWhenFirstSongHasMinRating() {
		theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 1));
		theTracks.add(new Track("song 2", 4));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 8));

		assertEquals(0, theTracks.getFirstIndexWithMinRating());
	}
	
	public void testShouldReturnThreeWhenLastSongHasMinRating() {
		theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 3));
		theTracks.add(new Track("song 2", 4));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 1));

		assertEquals(3, theTracks.getFirstIndexWithMinRating());
	}
	
	public void testShouldReturnOneWhenSecondSongHasMinRating() {
		theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 2));
		theTracks.add(new Track("song 2", 1));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 8));

		assertEquals(1, theTracks.getFirstIndexWithMinRating());
	}
	
		public void testShouldReturnOneWhenSecondAndLastSongHasMinRating() {
		theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 2));
		theTracks.add(new Track("song 2", 1));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 1));

		assertEquals(1, theTracks.getFirstIndexWithMinRating());
	}
}

I also include a picture of the error message I keep getting.
Thanx in advance.

Made this chage

public int getFirstIndexWithMinRating() {
        String minName = "";
        for(Track nextTrack : tracks) {
            if (nextTrack.getRating() == Track.MINIMUM_RATING){
            minName = nextTrack.getName();
            break;
            }
        }
        return indexOf(minName);
    }

Made this chage

public int getFirstIndexWithMinRating() {
        String minName = "";
        for(Track nextTrack : tracks) {
            if (nextTrack.getRating() == Track.MINIMUM_RATING){
            minName = nextTrack.getName();
            break;
            }
        }
        return indexOf(minName);
    }

Holy crap man I feel like such an idiot. Thank you so much. I spent almost two days on this. Thanx again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.