2010-07-07 119 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中删除标题并实现第一个...