Java登录验证不起作用?
我已经在代码中声明了所有相关的字符串变量。我的问题是我的登录验证。单击按钮时,我试图验证文本文件中是否存在字符串。其布局为“用户名:john25,密码:john25”。谁能告诉我到底哪里出了问题Java登录验证不起作用?,java,login,java.util.scanner,Java,Login,Java.util.scanner,我已经在代码中声明了所有相关的字符串变量。我的问题是我的登录验证。单击按钮时,我试图验证文本文件中是否存在字符串。其布局为“用户名:john25,密码:john25”。谁能告诉我到底哪里出了问题 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:]
if(evt.getSource()==jButton1){
boolean found = false;
username = ("");
password = ("");
filepath =("doctorFiles.txt");
username = jTextField1.getText();
password = jPasswordField1.getText();
try{
x = new Scanner(new File(filepath));
x.useDelimiter("[,\n]");
while(x.hasNext() && !found ) {
username = x.next();
password = x.next();
if(username.trim().equals(username.trim()) && password.trim().equals(password.trim()))
{
found = true;
if(found==true){
welcomePage goback = new welcomePage();
goback.setVisible(true);
dispose();
}
}
x.close();
System.out.println(found);
}
}
catch(Exception e)
{
System.out.println("Error is "+ e);
}
}
}
你为什么要在这里加入你自己的变量:
username = jTextField1.getText();
password = jPasswordField1.getText();
username = x.next();
password = x.next();
这没有任何意义,在你的假设中
if(username.trim().equals(username.trim()) && password.trim().equals(password.trim()))
您正在比较相同的变量,它将始终为真。
尝试创建新变量
String usernameFromTxt = x.next();
String passwordFromTxt = x.next();
if(usernameFromTxt .trim().equals(username.trim()) && usernameFromTxt .trim().equals(password.trim()))
为了改进这个问题,添加一些描述,描述你期望发生的事情与实际发生的事情。还包括出现的任何错误消息。通常,可以使用调试器或System.out.println()语句在每行代码处检查应用程序的状态。请看这里: