登录表格IF-ELSE运作不佳

时间:2017-03-24 19:56:15

标签: java swing login text-files

注意文本文件中的数据如下:

  • 客户-1-客户
  • coach-1-Sport Coach
  • coach2-1234-体育教练
  • customer2-1234-客户

问题 1。程序可以读取数据1和2。但是一旦它进入特定的菜单,它还会执行else语句,该语句将显示弹出窗口。 2。数据3& 4无法读取,它直接转到else语句,尽管用户名&密码输入正确。

private void btn_ConfirmActionPerformed(java.awt.event.ActionEvent evt) {                                            

    Admin = txt_Username.getText();
    Admin_Pass = txt_Password.getText();

    try
    {
        BufferedReader br = new BufferedReader(new FileReader(("USER_Account.txt")));

        String s;
        String TOU;
        String UIusername; //txt_Username.getText();
        String UIpassword; //txt_Password.getText();
    while((s = br.readLine()) != null)
    {   
       String tmp [] = s.split("-");

       UIusername = tmp[0];
       UIpassword = tmp[1];
       TOU = tmp[2];

        if (txt_Username.getText().equals(UIusername) && txt_Password.getText().equals(UIpassword) && TOU.equals("Sport Coach"))
        { JOptionPane.showMessageDialog(null,"Logged in as Coach!","",JOptionPane.INFORMATION_MESSAGE);
            SportCoach_Menu spMenu = new SportCoach_Menu();
            this.hide();
            spMenu.show();
        }
        else if (txt_Username.getText().equals(UIusername) && txt_Password.getText().equals(UIpassword) && TOU.equals("Customer"))
        { JOptionPane.showMessageDialog(null,"Logged in as Customer!","",JOptionPane.INFORMATION_MESSAGE);
            Customer_Menu cMenu = new Customer_Menu();
            this.hide();
            cMenu.show();
        }
        else if (Admin.equals("admin") && Admin_Pass.equals("1234"))
        {
            JOptionPane.showMessageDialog(null,"Logged in as Admin!","",JOptionPane.INFORMATION_MESSAGE);
            Staff_Menu sMenu = new Staff_Menu();
            this.hide();
            sMenu.show();
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Invalid Username / Password, please retry!","",JOptionPane.ERROR_MESSAGE);
            txt_Username.setText(null);
            txt_Password.setText(null);
        }        
    }
    br.close();  
    }
    catch(Exception e)
    {
     e.printStackTrace();
    }
}

非常感谢帮助!!

1 个答案:

答案 0 :(得分:0)

这很有效。我做了一些修改。问你是否不懂任何事情。

我没有阅读文本字段,而是硬编码了这些值。你应该改回来。

import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class Main {

    public static void main(String[] args) {

        String Admin = "coach2";
        String Admin_Pass = "1234";

        try {
            BufferedReader br = new BufferedReader(new FileReader(("USER_Account.txt")));

            String s;
            String TOU;
            String UIusername; //txt_Username.getText();
            String UIpassword; //txt_Password.getText();
            boolean loggedIn = false;
            while ((s = br.readLine()) != null && !loggedIn) {
                String tmp[] = s.split("-");

                UIusername = tmp[0];
                UIpassword = tmp[1];
                TOU = tmp[2];

                if (Admin.equals(UIusername) && Admin_Pass.equals(UIpassword) && TOU.equals("Sport Coach")) {
                    JOptionPane.showMessageDialog(null, "Logged in as Coach!", "", JOptionPane.INFORMATION_MESSAGE);
                    loggedIn = true;
                } else if (Admin.equals(UIusername) && Admin_Pass.equals(UIpassword) && TOU.equals("Customer")) {
                    JOptionPane.showMessageDialog(null, "Logged in as Customer!", "", JOptionPane.INFORMATION_MESSAGE);
                    loggedIn = true;
                } else if (Admin.equals("admin") && Admin_Pass.equals("1234")) {
                    JOptionPane.showMessageDialog(null, "Logged in as Admin!", "", JOptionPane.INFORMATION_MESSAGE);
                    loggedIn = true;
                }
            }
            if (!loggedIn)JOptionPane.showMessageDialog(null, "Invalid Username / Password, please retry!", "", JOptionPane.ERROR_MESSAGE);
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}