2010-07-07 79 views
0

我確實需要你的幫助。如何修改此代碼語句?

我有這樣的代碼:

File f1 = openFile("Choose file one", 
     JFileChooser.OPEN_DIALOG); 
    if (f1 == null) 
     return; 
    File f2 = openFile("Choose file two", 
     JFileChooser.OPEN_DIALOG); 
    if (f2 == null) 
     return; 

     File f3 = openFile("Choose destination", 
     JFileChooser.SAVE_DIALOG); 
    if (f3 == null) 
     return; 
JOptionPane.showMessageDialog(this, "Files are now joined in" 
      + f3.getName()); 

我想擺脫這樣的:

File f3 = openFile("Choose destination", 
     JFileChooser.SAVE_DIALOG); 
    if (f3 == null) 
     return; 

,並使用以下代碼語句,這樣一來,但不幸的是我得到的錯誤..!

OptionPane.showMessageDialog(this, "Files are now joined in" 
      + f1.getName() + f2.getName()); 

我該如何解決問題?

+1

哪個錯誤?我的意思是說,超過15個字符:哪些錯誤? – 2010-07-07 10:14:42

+0

你得到了什麼錯誤?您在JOptionPane中缺少J。 – 2010-07-07 10:14:54

+0

你實際上想通過你的改變實現什麼? – 2010-07-07 10:15:07

回答

0

你真的需要更具體一些,你會得到什麼錯誤?你的最終代碼看起來像是什麼樣的?

如果您在刪除聲明後引用f3,則會出現錯誤,正如Mathew所述,您錯過了JOptionPane中的「j」。

好運

0
JOptionPane.showMessageDialog(this, "Files are now joined in" 
     + f1.getName() + f2.getName()); 

應該編譯如果thisComposite一個子類。假設輸入文件F1和F2 path/file1.wavpath/file2.wav,該消息將

Files are now joined infile1.wavfile2.wav 
1

如果你想添加文件名,您需要將名字從擴展先被分割。使用'lastIndexOf method to find the '.' and substring`只得到名稱部分:

... 
    if (f2 == null) 
     return; 

    String newName = fileName(f1) + "+" + f2.getName(); // assuming extension of f2 
    // or newName = fileName(f1) + "+" + fileName(f2) + ".wav"; 
    File f3 = new File(newName); 
    ... 

private static String fileName(File file) { 
    String name = file.getName(); 
    int index = name.lastIndexOf('.'); 
    if (index != -1) { 
     name = name.substring(0, index); 
    } 
    return name; 
} 

類似得到擴展(name.substring(index+1)),並檢查這兩個擴展是相等的(如果需要)。

英語不是我的第一(第二既不)語言,更正,歡迎

編輯
但我不相信,只是連接兩個WAV文件將生成一個工作之一。我懷疑你需要從第二個WAV中刪除標題並實現第一個...