基于txt文件的java登录

基于txt文件的java登录,java,login,io,read-write,Java,Login,Io,Read Write,需要帮助!,我有一个任务,我需要一个登录txt的基础上,我的程序只读取第一行 图片看起来像这样 Luthfi Luthfi Fitra Fitra Fitra Khori Khori Fitra Syifa 但它只读取第一个,所以我只能使用顶级帐户登录 这是我的密码 public Login() { initComponents(); txtPass.setText(""); } public void Masuk(){ try { String lokasi

需要帮助!,我有一个任务,我需要一个登录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时,都会显示im成功,但也会显示用户名/密码错误

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

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

user.equals(username)
您正在将小写字符串与未修改的(混合大小写)字符串进行比较。这有点新,您能解释更多吗?对不起,我只是删除了toLowerString,但它仍然给我错误的提示password@Luthfi穆萨法检查我的答案