你好,我需要检查我的程序中的扩展名是否已经存在,以及它是否阻止它被输入到条目列表中,并弹出一个对话框。在下面的代码现在我可以不再插入任何东西,因为所有的东西都是一样的。我觉得我正在犯一个基本的愚蠢的错误,但我看不到它。检查两个字符串是否相同停止输入
private void addNewEntry()
{
// TODO add code to create a new entry
// should show appropriate error if unable to create the entry
// update the UI
// update the data file by calling updateDataFile
String fileExtension, program;
String repeatExtension;
fileExtension = txtAddExtension.getText();
if (!fileExtension.substring(0,1).equals("."))
fileExtension = "." + fileExtension;
repeatExtension = fileExtension;
program = txtAddProgram.getText();
if(fileExtension.equalsIgnoreCase(repeatExtension))
{
JOptionPane.showMessageDialog(txtList, "Matching extension", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
Association assoc = new Association(fileExtension, program);
assocList.add(assoc);
updateDataFile();
try {
doc.remove(0, doc.getLength());
doc.insertString(doc.getLength(), "Entry added\n" + assoc.toString() + "\n", null);
} catch (BadLocationException e) {
e.printStackTrace();
}
listAll();
}
}
'fileExtension.substring(0,1).equals检查之前,您要分配给
repeatExtension
一样fileExtension
(fileExtension) '只有在大小为1的文件扩展名(即一个字符长)的情况下才是真实的。 – Berger感谢您的答复,我更新了我的代码与我目前正在尝试,但仍然没有运气 –