如何打破这个 java 程序中的 while 循环
How to break the while loop in this java program
我正在使用 java 进行登录。身份验证不断循环并显示错误消息,直到它获得正确的用户名和密码。如何解决?我只希望它在找到时中断,只在找不到时显示未找到。
private void loginbtnActionPerformed(java.awt.event.ActionEvent evt) {
String username = usertxt.getText();
String password = passwordtxt.getText();
try
{
File login = new File("logindata.txt");
Scanner scan = new Scanner(login);
scan.useDelimiter("[,\n]");
while(scan.hasNext())
{
String user = scan.next();
String pass = scan.next();
if (username.equals(user.trim()) && password.equals(pass.trim()))
{
JOptionPane.showMessageDialog(null,"Welcome!"+username);
this.dispose();
new peopleoptions().setVisible(true);
break;
}
else
{
JOptionPane.showMessageDialog(null,"User not found, please register");
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"System Error");
}
}
我的评论作为代码,几乎没有重新思考:
...
scan.useDelimiter("[,\n]");
boolean found = false; // Memorize...
while (!found && scan.hasNext()) { // ...process until found or end ("until(x or y)" <=> "while (not x and not y)" <==> "while not(x or y)";)
String user = scan.next();
String pass = scan.next();
if (username.equals(user.trim()) && password.equals(pass.trim())) {
found = true; // .. don't break...
}
}
// show dialog (and do "the action") only after "processing database" ...
if (found) {
// log.info("yay");
JOptionPane.showMessageDialog(null,"Welcome!"+username);
new peopleoptions().setVisible(true);
} else { // ... AND not found!;)
JOptionPane.showMessageDialog(null,"Credentials invalid, please register new user or reset password");
}
// really??: this.dispose(); // in any case?? maybe: "finally"!
} catch( ...
鉴于手头的问题,归结为:
- 遍历文件,设置布尔标志
- 然后之后做正确的事情。
请“释放”您的资源(文件、扫描仪...)!!
How to Correctly Close Resources
与 java >= 8:
我正在使用 java 进行登录。身份验证不断循环并显示错误消息,直到它获得正确的用户名和密码。如何解决?我只希望它在找到时中断,只在找不到时显示未找到。
private void loginbtnActionPerformed(java.awt.event.ActionEvent evt) {
String username = usertxt.getText();
String password = passwordtxt.getText();
try
{
File login = new File("logindata.txt");
Scanner scan = new Scanner(login);
scan.useDelimiter("[,\n]");
while(scan.hasNext())
{
String user = scan.next();
String pass = scan.next();
if (username.equals(user.trim()) && password.equals(pass.trim()))
{
JOptionPane.showMessageDialog(null,"Welcome!"+username);
this.dispose();
new peopleoptions().setVisible(true);
break;
}
else
{
JOptionPane.showMessageDialog(null,"User not found, please register");
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"System Error");
}
}
我的评论作为代码,几乎没有重新思考:
...
scan.useDelimiter("[,\n]");
boolean found = false; // Memorize...
while (!found && scan.hasNext()) { // ...process until found or end ("until(x or y)" <=> "while (not x and not y)" <==> "while not(x or y)";)
String user = scan.next();
String pass = scan.next();
if (username.equals(user.trim()) && password.equals(pass.trim())) {
found = true; // .. don't break...
}
}
// show dialog (and do "the action") only after "processing database" ...
if (found) {
// log.info("yay");
JOptionPane.showMessageDialog(null,"Welcome!"+username);
new peopleoptions().setVisible(true);
} else { // ... AND not found!;)
JOptionPane.showMessageDialog(null,"Credentials invalid, please register new user or reset password");
}
// really??: this.dispose(); // in any case?? maybe: "finally"!
} catch( ...
鉴于手头的问题,归结为:
- 遍历文件,设置布尔标志
- 然后之后做正确的事情。
请“释放”您的资源(文件、扫描仪...)!!
How to Correctly Close Resources
与 java >= 8: