试图用Java创建一个Jukebox

时间:2016-08-16 16:41:59

标签: java

我正在上Java课程,我正在尝试创建一个" Jukebox"在Java中,将根据用户的命令播放三首歌曲。我试图用多个类来做这个,所以我确保放入两个类。这是我的代码:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class JukeBox
{
    JukeBox(String[] playList, String[] fileList)
    {
    }

    public int displayMenu() throws IOException
    {
      String token = "";
      Scanner in = new Scanner(System.in);
      File fileName = new File("PlayList.txt");
      Scanner inFile = new Scanner(fileName);
      while (inFile.hasNext())
      {
         token = inFile.nextLine( );
         System.out.println (token);
      }
      return in.nextInt();
    }

    public void playSong(int choice)throws IOException
    {
      File fileName = new File("PlayList.txt");
      Scanner inFile = new Scanner(fileName);
      if(choice == 1)
      {
         SimpleSound song1 = new SimpleSound("Radioative.wav");
         song1.play();
      }
      else if(choice == 2)
      {
          SimpleSound song2 = new SimpleSound("Rope.wav");
          song2.play();
      }
      else if(choice == 3)
      {
          SimpleSound song3 = new SimpleSound("Same Love.wav");
          song3.play();
      }

      else if(choice == 0)
      {
          inFile.close();
      }
    }
}

这是"文件处理程序"和测试者类:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
class FileHandler
{
    private String[] playList = new String[7];
    private String[] fileList = new String[7];
    private String file;

    FileHandler(String f) 
    {
        file = f;
    }

    public void readFile() throws IOException
    {
        int index = 0;
        File fileName = new File(file);
        Scanner inFile = new Scanner(fileName);
        while (inFile.hasNext())
        {
            playList[index] = inFile.nextLine();
            fileList[index] = inFile.nextLine();
            index++;
        }
        inFile.close();
    }

    public String[] getPlayList()
    {
        return playList;
    }

    public String[] getFileList()
    {
        return fileList;
    }
}//end of class 

public class JukeBoxTester
{
    public static void main(String[] args) throws IOException
    {
        int choice = 0;
        FileHandler fileHandler = new FileHandler("PlayList.txt");
        fileHandler.readFile();
        String [] playList = fileHandler.getPlayList();
        String [] fileList = fileHandler.getFileList();

        JukeBox myPod = new JukeBox(playList, fileList);
        choice = myPod.displayMenu();
        myPod.playSong(choice);

    }
}    

所以,我编译我的程序,没有收到语法错误;但是,当它运行时,我收到错误

  

java.util.NoSuchElementException:找不到行(in.java.util.Scanner)

所以,突出显示

  

String [] fileList = fileHandler.getFileList();

我很失落,如果按照我想要的方式运行,我仍然不会百分百肯定。但是,我试图一步一步地解决这个问题。请帮忙。此外,这是我正在使用的文本文件:

        Dylan's PlayList
********************************************
<1> Imagine Dragons     Radioactive
<2> Foo Fighters        Rope
<3> Macklemore          Same Love
********************************************
Enter your choice (1-3 or 0 to quit):       

我的音乐文件已经保存为.wav,随时可以播放。

0 个答案:

没有答案