0
我创建了一个用户登录对话框,用于比较它们存储在csv文件中的用户名和密码与他们在相应文本框内键入的内容。在登录出现之前,我使用JOptionPanes来显示我已经传递用户名称密码的返回语句。 JOptionPane显示名称和密码。但是我登录后出现,然后输入名称和密码进行比较,登录不成功。当我使用JOptionPane再次显示相同的用户名和密码时,它说两者都是空的。我已经缩小了我设置了一个单独的布尔方法的问题,我使用它来查看用户输入的内容是否等于返回方法的值,但由于某种原因,我的返回方法在比较之前返回NULL。java/swing-为什么我的返回方法在赋值后返回NULL?
有关为何发生这种情况的任何建议?再次,由于某种原因,我正在失去返回值的“getName()”和“getPass()”。
/**Read csv data. put data into array, then elements into variables */
String lines = "";
String unparsedFile = "";
FileReader fr = new FileReader("c:\\foo\\user.dat");
BufferedReader br = new BufferedReader(fr);
while((lines = br.readLine()) != null){
unparsedFile += lines;
}
br.close();
userData = unparsedFile.split(",");
String g = userData[0];
g1 = userData[0]; // return in String method "getName()"
this.name = g1;
h1 = userData[1]; // returned in String method "getPass()"
this.password = h1;
///////////////////////////////
/* check if what you entered is same as getName() and getPass() */
public static boolean authenticate(String username, String password){
String g2, h2;
g2 = cn.getName();
h2 = cn.getPass();
JOptionPane.showInputDialog(null, cn.getName() + " login " + cn.getPass());
if(username.equals(g2) && password.equals(h2)){
JOptionPane.showInputDialog(null, "True");
return true;
}
JOptionPane.showInputDialog(null, cn.getName() + " login false " + cn.getPass());
return false;
}
////////////////////////
/* Login: enter name and password, the check if it matches getName() and getPass() */
nam = tfUsername.getText();
passA = pfPassword.getText();
if(Login.authenticate(nam, passA)){
JOptionPane.showMessageDialog(UserLogin.this,
"Welcome, " + cn.getChatName() + ". Login successful.",
"Login",
JOptionPane.INFORMATION_MESSAGE);
succeeded = true;
dispose();
}
if(!nam.equals(cn.getName()) && !passA.equals(cn.getPass())){
JOptionPane.showMessageDialog(UserLogin.this, "Invalid username or password",
"Login unsuccessful", JOptionPane.ERROR_MESSAGE);
//reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;
}
}
});
如果您提供[SSCCE](http://pscode.org/sscce.html),您可能会得到答案。我们甚至不知道'cn'是什么。 – Howard 2012-03-25 09:20:14
@Howard cn只是一个类名。这三个部分是3个不同的类别。 – SpicyWeenie 2012-03-25 09:41:42
霍华德是正确的需要更多的代码cn是一个谜,我应该澄清,如果我们可以看到setName和setPassword被调用,这也会很好。 – 2012-03-25 09:46:29