简体   繁体   English

检测txt文件中的相同单词Java

[英]Detect same words in txt file Java

I'm very new to Java and I'm making a gui that has a "register" button and "check if ID is available button".我对 Java 很陌生,我正在制作一个带有“注册”按钮和“检查 ID 是否可用按钮”的 gui。

The register button is suppossed to add the ID, Password, Username to the txt file.注册按钮应该是在txt文件中添加ID、密码、用户名。 As you can see in the image below the register is working well, The "check if ID is available button" is not.正如您在下图中看到的,寄存器运行良好,“检查 ID 是否可用按钮”不是。 When I register with "ID: qwe PW: 123 Username:abc", "qwe 123 abc " is added to my txt file.当我使用“ID: qwe PW: 123 Username:abc”注册时,“qwe 123 abc”被添加到我的 txt 文件中。 When I type in "qwe" in ID again and press the "ID available check" button it shows me it's available.当我再次在 ID 中输入“qwe”并按下“ID 可用检查”按钮时,它显示它可用。 But when I type in "qwe 123 abc " it says it's not available.但是当我输入“qwe 123 abc”时,它说它不可用。

I think it has something to do with the readLine but I have no idea how to make it read in seperate words.我认为它与 readLine 有关,但我不知道如何使它以单独的单词阅读。 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?有什么我可以添加的让它以单独的单词读取该行并使其在我输入相同的 ID 并再次按检查 ID 按钮时显示它不可用?

This is the check available ID button code:这是检查可用的 ID 按钮代码:

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图形用户界面图像

You are trying to build a user/password database with a text file.您正在尝试使用文本文件构建用户/密码数据库。 From a function perspective, there is nothing wrong about it.从 function 的角度来看,没有任何问题。 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.如果您愿意,可以使用任何关系系统,例如 MariaDB、Apache Derby 或一些 H2 数据库。 But the interface for all of them would be JDBC and SQL - much easier to learn and maintain than creating your own engine.但是它们的接口都是 JDBC 和 SQL - 比创建自己的引擎更容易学习和维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM