【发布时间】:2017-11-28 07:06:23
【问题描述】:
没有错误,但是代码在从文本文件中获取用户名和密码后停止,甚至没有读取下一行来检查用户的角色 你能帮我解决这种错误吗
数据
Bill;admin;admin;Admin
Miguel;SMMiguel;SM1234;Sales Manager
Josh;PMJosh;PM1234;Purchase Manager
这是 User.txt 中的内容
private void initialize() {
Login_Frame = new JFrame();
Login_Frame.setTitle("Login");
Login_Frame.setBounds(100, 100, 471, 415);
Login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Login_Frame.getContentPane().setLayout(null);
User_txt = new JTextField();
User_txt.setBounds(145, 93, 216, 22);
Login_Frame.getContentPane().add(User_txt);
User_txt.setColumns(10);
JButton Login_btn = new JButton("Log in");
Login_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// name of the file
String fileName = "User.txt";
String Uname = User_txt.getText();
String Psd = Pass_txt.getText();
Item_Entry ie = new Item_Entry();
ie.set_box(Uname);
String line = null;
String SMrole = "Sales Manager";
String PMrole = "Purchase Manager";
try {
//read and buffer the file
FileReader fileReader = new FileReader(fileName);
BufferedReader br = new BufferedReader(fileReader);
while ((line = br.readLine()) !=null)
{
String[] getdata = line.split(";");
if(Uname.equals(getdata[1]) && Psd.equals(getdata[2]))
{
JOptionPane.showMessageDialog(null, "Get the data for user and pass");
if(PMrole.equals(getdata[3]))
{
JOptionPane.showMessageDialog(null, "Login Successful");
Purchase_Manager_Access PMAccess = new Purchase_Manager_Access();
PMAccess.setVisible(true);
Username = User_txt.getText();
}
else if(SMrole.equals(getdata[3]))
{
Sales_Manager_Access SMAccess = new Sales_Manager_Access();
SMAccess.setVisible(true);
Username = User_txt.getText();
}
}
}
}
catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
}
}
});
Login_btn.setBounds(94, 210, 109, 49);
Login_Frame.getContentPane().add(Login_btn);
JButton Exit_btn = new JButton("Exit");
Exit_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Exit_btn.setBounds(239, 210, 109, 49);
Login_Frame.getContentPane().add(Exit_btn);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(59, 96, 78, 16);
Login_Frame.getContentPane().add(lblUsername);
lblPassword = new JLabel("Password");
lblPassword.setBounds(59, 145, 56, 16);
Login_Frame.getContentPane().add(lblPassword);
Pass_txt = new JPasswordField();
Pass_txt.setBounds(145, 142, 216, 22);
Login_Frame.getContentPane().add(Pass_txt);
}
【问题讨论】:
-
您的程序到底在哪里停止?目前尚不清楚您的问题到底是什么,并且此示例代码包含编译错误。
标签: java if-statement runtime-error bufferedreader