基于 txt 文件登录 java

Login on java based from txt file

需要帮助!,我有一个作业需要基于 txt 进行登录,我的程序只读取第一行

txt 看起来像这样

Luthfi Luthfi Fitra
Fitra Fitra Khori
Khori Fitra Syifa

但它只读到第一个,所以我只能使用顶级帐户登录 这是我的代码

        public Login() {
    initComponents();
    txtPass.setText("");
}

public void Masuk(){
try {
String lokasi = "D:/settings.txt";
String username = txtUser.getText();
String password = txtPass.getText();


        FileReader fr = new FileReader(lokasi);
        BufferedReader br = new BufferedReader(fr);
        String line,user,pass;
        boolean isLoginSuccess = false;
       while ((line = br.readLine()) != null) {
           user = line.split(" ")[1].toLowerCase();
    pass = line.split(" ")[2].toLowerCase();
           if (user.equals(username) && pass.equals(password)){
               isLoginSuccess = true;
               this.dispose();
               MainMenu mm = new MainMenu();
               mm.setLocationRelativeTo(null);
               mm.setVisible(true);
               break;
           }
           else{
               JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD SALAH","WARNING!!",JOptionPane.WARNING_MESSAGE);
               break;
           }
       }
       fr.close();

}catch(Exception e){
e.printStackTrace();
    }
        }

另外,为什么每次我输入正确的用户名和 ID 时都显示我成功了,但它显示 USERNAME/PASSWORD 也是错误的

您在从文件中获取子字符串时遇到问题,更改代码如下片段

        user = line.substring(0, 6).toLowerCase();
        pass = line.substring(7, 12).toLowerCase();

更新:根据您的评论,我正在更改我的解决方案

1) 像这样更改文件格式

Luthfi Luthfi Hehe
Fitra Luthfi Khori
Syifa Khori Luthfi
Khori Syifa Luthfi

此处每行第一个词是用户名,第二个词是密码

2) 像下面这样更改您的代码

            user = line.split(" ")[1].toLowerCase();
            pass = line.split(" ")[2].toLowerCase();

3) 如果任何用户名和密码匹配,则进行登录并退出 while 循环

再次更新

package problems;

import java.io.BufferedReader;
import java.io.FileReader;

public class FileRd {

    public static void main(String args[]) {
        try {
            String lokasi = "D:/settings.txt";
            String username = txtUser.getText();
            String password = txtPass.getText();

            FileReader fr = new FileReader(lokasi);
            BufferedReader br = new BufferedReader(fr);
            String line, user, pass;
            boolean isLoginSuccess = false;
            while ((line = br.readLine()) != null) {
                user = line.split(" ")[1].toLowerCase();
                pass = line.split(" ")[2].toLowerCase();
                if (user.equals(username) && pass.equals(password)) {
                    isLoginSuccess = true;
                    this.dispose();
                    MainMenu mm = new MainMenu();
                    mm.setLocationRelativeTo(null);
                    mm.setVisible(true);
                    break;
                } 
            }
            if (!isLoginSuccess) {
                JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD WRONG", "WARNING!!", JOptionPane.WARNING_MESSAGE);
            }
            fr.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}