Java 如何修改此代码语句?
我确实需要你的帮助 我有这个密码:Java 如何修改此代码语句?,java,Java,我确实需要你的帮助 我有这个密码: 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; F
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());
我如何解决这个问题?你真的需要更具体一些,你会遇到什么错误?你的最终代码看起来怎么样 如果在删除声明后引用f3,您将得到一个错误,同样,正如Mathew所提到的,您在JOptionPane中遗漏了“j” 祝你好运
JOptionPane.showMessageDialog(this, "Files are now joined in"
+ f1.getName() + f2.getName());
如果此
是复合
的子类,则应编译。假设输入文件f1和f2是path/file1.wav
和path/file2.wav
,则消息将
Files are now joined infile1.wavfile2.wav
如果要添加文件名,首先需要将文件名与扩展名分开。使用“lastIndexOf方法查找“.”和
子字符串`仅获取名称部分:
...
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。我怀疑您需要从第二个WAV中删除标题并实现第一个…哪些错误?我的意思是,用超过15个字符来表达:哪些错误?你有哪些错误?您缺少JOptionPane中的J。您实际希望通过更改实现什么?仅仅因为它“丑陋”并不意味着存在语法、错误或错误。。。只是可读性。如果有效-将其发送给您的客户。如果有必要,请稍后返回并重构。我希望能够为连接的文件(f3)提供一个组合名称。我的意思是,如果a选择第一个文件名为“chord.wav”,然后我选择第二个文件名为“phone.wav”,连接后的结果文件应该是:“chord+phone.wav”。实际上,用户不应该能够设置文件名,但是输出文件名应该和我上面提到的一样。