Greg Jones
Greg Jones

Reputation: 53

Checking if two strings are the same to stop inputs

Hello i'm needing to check if an extension in my program already exists and if it does to stop it from being inputted into the list of entries and pop up a dialog box. At the moment with the code below i can now no longer insert anything as everything is said to be the same. I feel like i'm making a basic idiotic mistake but i can't see it.

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

}

Upvotes: 0

Views: 66

Answers (1)

gustf
gustf

Reputation: 2017

You are assigning repeatExtension to the same as fileExtension just before checking if they are equal so the check will always be true

     repeatExtension = fileExtension;

Upvotes: 1

Related Questions