比较JTextField和文本文件中的文本

时间:2019-03-08 13:24:06

标签: java swing if-statement text-files jtextfield

即使所比较的变量似乎相同,也没有输入我的if语句。用户的输入来自JTextField和JPasswordField,文件是.txt文件。

This shows the two entries from the user, username and password on the left and the inputs from the text file of username and password on the right.

它们似乎在这里匹配,但是当我在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);
  }
}

}

0 个答案:

没有答案