Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Efficiency! Building the Model

Michael Walker
Michael Walker
45,428 Points

Modifying the Code

Is there any easy way to modify the code so we don't have the common bug listing one song as songs as Craig pointed out in the video? Here's what I have, but I'm sure there is a much easier way to access the count. Any ideas?

if (songBook.getSongCount() == 1) { System.out.printf("There is %d song. %n", songBook.getSongCount()); } else { System.out.printf("There are %d songs. %n", songBook.getSongCount()); }

2 Answers

deckey
deckey
14,630 Points

Hi Michael, your approach is fine, except some redundant code.

Since we already established that there is 1 song in the first condition, then there is no need to call the method again just for printing results, so:

if (songBook.getSongCount()== 1) {
    System.out.println("There is 1 song. %n"); 
}

cheers

Michael Walker
Michael Walker
45,428 Points

lol, I didn't even think of that. You get so used to calling methods all the time, it didn't even occur to me I didn't need a placeholder there. Thanks deckey!