Java 从文本文件读取数据后,代码突然停止
没有错误,但代码在从文本文件获取用户名和密码后停止,甚至不读取下一行来检查用户的角色 你能帮我解决这种错误吗 资料 这是User.txt中的内容Java 从文本文件读取数据后,代码突然停止,java,if-statement,runtime-error,bufferedreader,Java,If Statement,Runtime Error,Bufferedreader,没有错误,但代码在从文本文件获取用户名和密码后停止,甚至不读取下一行来检查用户的角色 你能帮我解决这种错误吗 资料 这是User.txt中的内容 private void initialize() { Login_Frame = new JFrame(); Login_Frame.setTitle("Login"); Login_Frame.setBounds(100, 100, 471, 415); Login_Frame.setDefaultCloseOpe
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);
}
您需要分离缓冲读取器,然后使用for循环来获得所需的结果。这将停止错误 我建议首先将文件存储到数组列表中
BufferedReader in = new BufferedReader(new FileReader(file_name));//replace file_name with your "User.txt"
String line_reader = null; // this is the string that reads each line
while((line_reader = in.readLine()) != null) // loop to add content to array list
{
file_content_list.add(line_reader);
}
现在,使用以下命令将数组列表转换为字符串数组:
String[] file_contents = new String[0];
// CONVERSION ARRAY LIST -> ARRAY
file_contents = file_content_list.toArray(new String[0]);
然后从字符串数组中创建一个字符串:
// CONVERSION ARRAY -> STRING
String string_file_content = null;
string_file_content = Arrays.toString(file_contents);
现在,您可以操作该字符串并将其拆分为新的split_内容数组
String[] getdata = new String[0];
getdata = str_file_contents.split(";");
这里是您使用for循环解决问题的地方:
下面是未经测试的伪代码,应仅用于教育目的
我已经包含了这个for循环,因为我不确定您是否希望它在用户输入不同的用户名或密码组合时出现
for(int i = 0,j=1; i <= string_file_content.length; i++, j++)
{
if(Uname.equals(getdata[i]) && Psd.equals(getdata[j]))
{
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();
}
字符串数组从0开始,因此
Uname.equals(getdata[1])
将是一个密码,Psd.equals(getdata[2])
将是第二个用户名,这将导致问题,我已在上面修复。检查伪代码。我设法修复了它,伙计们,问题出在User.txt上,因为它没有分号,所以无法将它拆分为数组并读取它
感谢您尝试修复它,伙计:)您的程序具体在哪里停止?不清楚您的问题到底是什么,此示例代码包含编译错误。
for(int i = 0,j=1; i <= string_file_content.length; i++, j++)
{
if(Uname.equals(getdata[i]) && Psd.equals(getdata[j]))
{
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();
}
if(Uname.equals(getdata[1]) && Psd.equals(getdata[2]))