2014-04-08 36 views
0

我已經創建了我的聲音類的用戶界面。它看起來應該工作得很好,因爲聲音類完美地工作。我不知道爲什麼,但當我嘗試運行READ命令時,出現錯誤,說沒有這樣的文件或目錄存在。該文件確實存在,並且與程序位於同一文件夾中。繼承人的代碼:用戶界面不讀取文件

public class SoundUI 
{ 
public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); 
    Sound s = new Sound(); 

    final String MENU = "R)ead , W)rite , B)ackwards , L)engthen , S)horten , I)ncrease Vol. ," + 
     "D)ecrease Vol. , Q)uit"; 
    char cmd = ' '; 

    System.out.println(MENU); 
    //User interface commands for sound 
    do{ 

     cmd = input.next().charAt(0); 
     if(cmd == 'R'){ //read 
      System.out.println("Enter file name with quotes around it"); 
      String fileName = input.next(); 
      s.wavRead(fileName); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
     else if(cmd == 'W'){ //save 
      System.out.println("Name of file you want to save as"); 
      String fileName = input.next(); 
      s.wavSave(fileName); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
     else if(cmd == 'B'){ //reverse 
      s.reverse(); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
     else if(cmd == 'L'){ //lengthen 
      s.lengthen(); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
     else if(cmd == 'S'){ //shorten 
      s.shorten(); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
     else if(cmd == 'I'){ //increase volume 
      System.out.println("Enter percent"); 
      double percent = input.nextDouble(); 
      s.increaseVol(percent); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
     else if(cmd == 'D'){ //decrease volume 
      System.out.println("Enter percent"); 
      double percent = input.nextDouble(); 
      s.reduceVol(percent); 
      System.out.println(MENU); 
      cmd = input.next().charAt(0); 
     } 
    } while(cmd !='Q'); //quit 
} 

}

+0

「附帶引號」部分聽起來對我很可疑。你真的在用引號嗎?如果是這樣,爲什麼?那麼'wavRead'方法是做什麼的?你已經發布你的菜單代碼,但這不是代碼失敗,可​​能是... –

+0

@JonSkeet我懷疑它是;特別是輸入問題。海報應該在打印出文件名之前先閱讀文件名,看看它是否有用。 – chrylis

+0

public void wavRead(String fileName)this.samples = WavIO.read(fileName); } – user3511198

回答

0

的代碼必須着眼於src文件夾的頂部。您可以使用以下代碼知道代碼正在查找哪個目錄: System.out.println(new File(".").getAbsoluteFile());

+0

此代碼不起作用。我如何正確輸入它? – user3511198

0

首先 - 寫出此行後面的文件名變量中包含的內容。

String fileName = input.next(); 

然後檢查s.wavRead方法中會發生什麼。 順便說一句,Sound類從哪裏來?這是你的課嗎?如果是,請調試wavRead方法並檢查那裏發生了什麼。

+0

這是我的課。把它作爲一項任務。當我在閱讀文件「bye8」後打印this.samples時,它給了我一個地址。 – user3511198