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

KaraokeMachine

why do we need that???

return choice -1;

Can you post the whole function?

package com.teamtreehouse;

import com.teamtreehouse.model.Song; import com.teamtreehouse.model.SongBook;

import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.List;

import java.util.ArrayList;

public class KaraokeMachine { private SongBook mSongBook; private BufferedReader mReader; private Map<String, String> mMenu;

public KaraokeMachine(SongBook songBook){ mSongBook = songBook; mReader = new BufferedReader(new InputStreamReader(System.in)); mMenu = new HashMap<String, String>(); mMenu.put("add", "Add a new song to the song book"); mMenu.put("choose", "Choose a song to sing!"); mMenu.put("quit", "Give up. Exit the program"); }

private String promptAction() throws IOException{ System.out.printf("There are %d songs available. Your option are : %n", mSongBook.getSongCount()); for(Map.Entry<String, String> option : mMenu.entrySet()){ System.out.printf("%s - %s %n", option.getKey(), option.getValue()); } System.out.print("What do you want to do: "); String choice = mReader.readLine(); return choice.trim().toLowerCase(); }

public void run(){ String choice = ""; do{ try{ choice = promptAction(); switch(choice){ case "add": Song song = promptNewSong(); mSongBook.addSong(song); System.out.printf("%s added! %n%n", song); break; case "choose": String artist = promptArtist(); Song artistSong = promptSongForArtist(artist); //TODO: add to a song queue System.out.printf("You chose: %s %n", artistSong); break; case "quit": System.out.println("Thanks for playing!"); break; default: System.out.printf("Unknown choice: '%s'. Try again. %n%n%n", choice);

   }

 }catch(IOException ioe){
  System.out.println("Problem with input"); 
  ioe.printStackTrace();
 }

}while(!choice.equals("quit"));

}

private Song promptNewSong()throws IOException{ System.out.print("Enter the artist's name: "); String artist = mReader.readLine(); System.out.print("Enter the title: "); String title = mReader.readLine(); System.out.print("Enter the video URL: "); String videoUrl = mReader.readLine(); return new Song(artist, title, videoUrl); }

private String promptArtist() throws IOException { System.out.println("Available artists:"); List<String> artists = new ArrayList<>(mSongBook.getArtists()); int index = promptForIndex(artists); return artists.get(index); }

private Song promptSongForArtist(String artist) throws IOException { List<Song> songs = mSongBook.getSongsForArtist(artist); List<String> songTitles = new ArrayList<>(); for (Song song : songs){ songTitles.add(song.getTitle()); } int index = promptForIndex(songTitles); return songs.get(index); }

private int promptForIndex(List<String> options) throws IOException{ int counter = 1; for(String option : options){ System.out.printf("%d.) %s %n", counter, option); counter++; } String optionAsString = mReader.readLine(); int choice = Integer.parseInt(optionAsString.trim()); System.out.print("Your choice: "); return choice -1; }

}

1 Answer

Code

private int promptForIndex(List<String> options) throws IOException{
    int counter = 1; 
    for(String option : options){
        System.out.printf("%d.) %s %n", counter, option);  // counter = 1 because list can't be zero based to the user but index of option = 0 when the loop starts
        counter++;
    }
        System.out.print("Your choice: ");     // say input is 1 from the user 
        String optionAsString = mReader.readLine();
        int choice = Integer.parseInt(optionAsString.trim());
    return choice - 1; }    // then input - 1 or 1-1 =0  i.e first element is returned

}

The method returns a list of options to the user and as we know it would be pretty stupid to start a list of options with a 0. instead a variable counter is created starting from 1 not 0 for displaying list starting from 1. but the arrays list doesn't work that way they starts with index 0 not 1 so when a user wants chooses a option in the array list the choice must subtract 1 from it in order to return the correct option.

thx i got it!