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! Queueing

Lucas Frixione
Lucas Frixione
1,332 Points

I've been stuck for days with a problem i have no idea how to solve

i have followed the tutorials step by step and i can't figure out what is the problem, there are 4 and they are all related of that i'm sure: line 35:

error: ';' expected private String promptAction() throw IOException{

line 79:

error: ';' expected private Song promptNewSong() throw IOException{

line 89:

error: ';' expected private String promptArtist() throw IOException{

line 109:

error ';' expected private int promptForIndex(List<String> options)throw IOException{

at first i thought it was the import of java.io.IOException but i have imported it : here it goes the code

package com.teamtreehouse;
import com.teamtreehouse.model.SongBook;
import com.teamtreehouse.model.Song;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayDeque;
import java.util.Queue;

public class KaraokeMachine{

  private SongBook mSongBook;
  private BufferedReader mReader;
  private Map<String,String> mMenu;
  private Queue<Song> mSongQueue;

  public KaraokeMachine(SongBook songBook){
mSongBook=songBook;
 // esto convierte bytes a chars cuando realmente lo necesitamos
//para ayudar a la eficiencia
    mReader= new BufferedReader(new InputStreamReader(System.in));
    mSongQueue = new ArrayDeque<Song>();
    mMenu = new HashMap<String,String>();
    // los mapas string string sirven mucho para hacer menus
    mMenu.put("add","Add a new son to the song book");    
    mMenu.put("play","Play next song in the queue");
    mMenu.put("choose","Choose a song to sing!");
    mMenu.put("quit","Give up. Exit the program");
  }

private String promptAction()throw IOException{
  //no es mi problema la exception
System.out.printf("There are %d songs available and %d in the queue. your options are%n",mSongBook.getSongCount(),mSongQueue.size());
  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();//trim recorta los espacios al final y al principio
  }

  public void run(){
  String choice = "";
  do {
    try{
  choice = promptAction();//en vez de hacer ifs usamos switch
      switch(choice){
    case "add":
      Song song =promptNewSong();
      mSongBook.addSong(song);
      System.out.printf("%s has been added! %n%n",song);
      break;
        case"choose":
        String artist = promptArtist();
        Song artistSong=promptSongForArtist(artist);
        //TODO: ADD TO A SONG QUEUE,done
        mSongQueue.add(artistSong);
        System.out.printf("you chose: %s %n",artistSong);
        break;
        case "play":
        playNext();
        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()throw 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 url =mReader.readLine();
  return new Song(artist,title,url);   
  }

  private String promptArtist() throw 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());
  }
    System.out.printf("Available songs for %s: %n",artist);
  int index=promptForIndex(songTitles);
  return songs.get(index);
  }


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

  public void playNext(){
  Song song = mSongQueue.poll();
  if(song == null){
  System.out.println("Sorry there are no songs in the queue. Use choose from the menu to add some");
  }else {
  System.out.printf("%n%n%n Open %s to hear %s by %s %n%n%n" ,song.getVideoUrl()
         ,song.getTitle()
          ,song.getArtist());
  }

  }
}

please help me i cant move forward if i don't finish with this and i'm getting really frustrated, i really want to finish the course but it has been 5 days and still nothing, i believe the workspaces are extremely buggy and need tons of fixing

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Your function declarations use throw instead of throws. throw is the actual act of throwing an exception. In other words it's a code statement which is expected to end in a semi-colon. throws is what you want to use to say "It throws an exception."