(TXT File) Java GUI Login

Tags: , ,



I want to login from a text file called “members.txt” which using 2nd (username) and third (password) line with “/” delimiter. But when I run it, it seems they recognize all of text file’s account in sequence. Please help. Here’s my code.

btn1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            String s;
            String bypassid = "guest";
            String bypasspw = "guest";
            String[] array;
            boolean isLogin= false; // 포기
            BufferedReader br = new BufferedReader(new FileReader("members.txt"));;
            while((s=br.readLine())!=null) {
                array=s.split("/");
                if(txtID.getText().equals(array[1])&&txtPass.getText().equals(array[2])){
                    JOptionPane.showMessageDialog(null, "로그인 되셨습니다");
                    break;
                } else if(array.length != 0 && bypassid.equals(txtID.getText())&&bypasspw.equals(txtPass.getText())){
                    JOptionPane.showMessageDialog(null, "로그인 되셨습니다");
                    break;
                } else {
                    JOptionPane.showMessageDialog(null, "계정 정보를 다시 확인해주세요.");
                }
            }
            br.close();
        } catch (IOException e10) {
                // TODO Auto-generated catch block
            e10.printStackTrace();
        }
    }
});

Answer

Actually you are reading every line and if user/password doesn’t match, you print error message in else {} block. You can just set boolean variable isLogin once and see if isLogin is false, print error message once outside loop. Below is the code snippet for that. Replace your actionPerformed method with code below

public void actionPerformed(ActionEvent e) {
            try {
                String s;
                String bypassid = "guest";
                String bypasspw = "guest";
                String[] array;
                boolean isLogin= false; // 포기
                BufferedReader br = new BufferedReader(new FileReader("members.txt"));
                while((s=br.readLine())!=null) {
                    array=s.split("/");
                    if(txtID.getText().equals(array[1])&&txtPass.getText().equals(array[2])){
                        JOptionPane.showMessageDialog(null, "로그인 되셨습니다");
                        isLogin = true;
                        break;
                    } else if(array.length != 0 && bypassid.equals(txtID.getText())&&bypasspw.equals(txtPass.getText())){
                        JOptionPane.showMessageDialog(null, "로그인 되셨습니다");
                        isLogin = true;
                        break;
                    }
                }
                if(!isLogin) {
                    JOptionPane.showMessageDialog(null, "계정 정보를 다시 확인해주세요.");
                }
                br.close();
            } catch (IOException e10) {
                // TODO Auto-generated catch block
                e10.printStackTrace();
            }
        }

Just be careful about what @David Kroukamp mentioned in comment



Source: stackoverflow