Detect same words in txt file Java

Viewed 23

I'm very new to Java and I'm making a gui that has a "register" button and "check if ID is available button".

The register button is suppossed to add the ID, Password, Username to the txt file. As you can see in the image below the register is working well, The "check if ID is available button" is not. When I register with "ID: qwe PW: 123 Username:abc", "qwe 123 abc " is added to my txt file. When I type in "qwe" in ID again and press the "ID available check" button it shows me it's available. But when I type in "qwe 123 abc " it says it's not available.

I think it has something to do with the readLine but I have no idea how to make it read in seperate words. Is there anything I can add to make it read the line in seperate words and make it show it's not available when I type in the same ID and press check ID button again?

This is the check available ID button code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        BufferedReader br = null;
        String checkid = jTextField1.getText();
        String readid = null;
        var allid = new ArrayList<String>();
        try{
            br = new BufferedReader(new FileReader("member.txt"));
            while((readid=br.readLine()) != null){
                allid.add(readid);
            }
            br.close();
            if(allid.contains(checkid)){
                JOptionPane.showMessageDialog(null, "not available");
            }else{
                JOptionPane.showMessageDialog(null, "available");
            }
        }catch (Exception ex) {
        }
    }

GUI image

1 Answers

You are trying to build a user/password database with a text file. From a function perspective, there is nothing wrong about it. But with growing data your performance will become a painpoint for sure.

You will have to implement a sorted list or perform some indexing or optimized other algorithms just to manage that data. Why not switch to a database?

You can use any relational system, like MariaDB, Apache Derby or some H2 database if you prefer. But the interface for all of them would be JDBC and SQL - much easier to learn and maintain than creating your own engine.

Related