2017-06-07 138 views
0

你好,我需要检查我的程序中的扩展名是否已经存在,以及它是否阻止它被输入到条目列表中,并弹出一个对话框。在下面的代码现在我可以不再插入任何东西,因为所有的东西都是一样的。我觉得我正在犯一个基本的愚蠢的错误,但我看不到它。检查两个字符串是否相同停止输入

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(); 
} 

} 
+3

'fileExtension.substring(0,1).equals检查之前,您要分配给repeatExtension一样fileExtension(fileExtension) '只有在大小为1的文件扩展名(即一个字符长)的情况下才是真实的。 – Berger

+0

感谢您的答复,我更新了我的代码与我目前正在尝试,但仍然没有运气 –

回答

1

只是如果他们是平等的,因此检查将永远是真正的

 repeatExtension = fileExtension; 
+0

我将如何改变这种结合的想法,我想阻止用户添加2相同的扩展类型? –

+0

将分机存储在列表中,并检查新分机是否存在于列表中 –

相关问题