即使所比较的变量似乎相同,也没有输入我的if语句。用户的输入来自JTextField和JPasswordField,文件是.txt文件。
它们似乎在这里匹配,但是当我在if语句中比较它们时,它永远不会返回true并输入if,它只会继续循环遍历文件中的每一行。为什么会这样?
感谢您的帮助。
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Enter") {
String userName = userNameEntry.getText();
String password = new String(userPassword.getPassword());
try {
FileReader readFile = new FileReader("cars-users.txt");
BufferedReader readLogin = new BufferedReader(readFile);
String line;
while((line = readLogin.readLine()) != null){
String[] credentials = line.split(",");
JOptionPane.showMessageDialog(null, userName + " " + password + " " + credentials[0] + " " + credentials[1]);
if(userName == credentials[0] && password == credentials[1]){
JOptionPane.showMessageDialog(null, "Welcome, " + userName);
setRole(credentials[2]);
break;
}
}readLogin.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "File Error \n" + ex);
}
}
}