我必须实现登录系统,为此,在创建帐户时,我将数据保存在文本文件中。数据格式为{username} {password} {bestScore}。但是当涉及到登录时,我有更多的用户。我的算法检查文本文件的每一行,如果用户存在,请转到主菜单面板。如果用户名错误,请打开joptionpane告诉用户用户名不正确或密码相同。问题是它在所有情况下都会对文件的第一行执行,而while循环会中断。那不是我的主意。我想检查整个文本文件中的用户名和密码匹配。不适用于文件的每一行。这是我的代码(不按我想要的方式工作):
private static final Pattern usernameAndPasswordPattern = Pattern.compile("^(\\S+) (\\S+) ([0-9]+)$");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\Niki\\Desktop\\Java Projects\\QuizGame\\QuizGame\\usernames.txt"))))) {
String line = br.readLine();
while (line != null) {
Matcher userNameAndPasswordMatcher = usernameAndPasswordPattern.matcher(line);
if (userNameAndPasswordMatcher.matches()) {
String username = userNameAndPasswordMatcher.group(1);
String password = userNameAndPasswordMatcher.group(2);
String bestScore = userNameAndPasswordMatcher.group(3);
String pwd = new String(passField.getPassword());
if (username.equals(usernameField.getText()) && !password.equals(pwd)) {
int result = JOptionPane.showConfirmDialog(null, "Incorrect password!", "Login incomplete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
passField.setText("");
}
}
else if (!username.equals(usernameField.getText()) && password.equals(pwd)) {
int result = JOptionPane.showConfirmDialog(null, "Incorrect username!", "Login incomplete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
usernameField.setText("");
}
}
else if (!username.equals(usernameField.getText()) && !password.equals(pwd)) {
int result = JOptionPane.showConfirmDialog(null, "This account don't exist!", "Login incomplete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
usernameField.setText("");
passField.setText("");
}
}
else if (username.equals(usernameField.getText()) && password.equals(pwd)) {
gd.setCurrentUser(username);
gd.setBestScore(Integer.parseInt(bestScore));
rdialog = new RedirectingDialog(frame);
rdialog.setVisible(true);
}
}
line = br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
在这里你可以看到,在第一行输入中,我将转到4个案例中的一个,而while循环将停止。但是如果用户名在第二行,例如我得到了带有消息"不正确的用户名"的对话框。我的一个想法是将文件中的所有内容附加到StringBuilder
,然后附加.contains
以检查用户名和密码是否存在,但我觉得它有点不对,因为如果我将用户名和密码与包含匹配将检查整个文本文件中的组合。 E.g用户名可以在第一行,密码可以在第3行,并且将匹配。那不是我想要的。他们必须在同一条线上才能进行比赛。
答案 0 :(得分:3)
首先,我认为您需要精确地满足您的要求 - 然后在计划中考虑对错是更容易的。
我认为用户名是正确的,然后它会在文件中出现一次。如果用户名不正确,则很可能不会出现在文件中;但是,如果名字彼此有点不相同,我们不能排除有人意外输入别人的用户名的可能性。
在!username.equals(usernameField.getText())
您不应该执行任何操作的两种情况下,只需继续在文件中搜索正确的用户名。
找到正确的用户名后,您可以执行必须执行的操作,具体取决于密码是否正确。之后,如果您愿意,可以突破循环,因为您知道稍后在文件中不会再找到用户名。
最后,如果您在没有找到用户名的情况下完成了整个文件,则应该发出有关用户名不正确的消息。
在代码中,类似这样(未经测试):
boolean userNameFound = false;
while (line != null) {
Matcher userNameAndPasswordMatcher = usernameAndPasswordPattern.matcher(line);
if (userNameAndPasswordMatcher.matches()) {
String username = userNameAndPasswordMatcher.group(1);
String password = userNameAndPasswordMatcher.group(2);
String bestScore = userNameAndPasswordMatcher.group(3);
String pwd = new String(passField.getPassword());
if (username.equals(usernameField.getText())) {
userNameFound = true;
if (password.equals(pwd)) {
gd.setCurrentUser(username);
gd.setBestScore(Integer.parseInt(bestScore));
rdialog = new RedirectingDialog(frame);
rdialog.setVisible(true);
} else { // password does not match
int result = JOptionPane.showConfirmDialog(null, "Incorrect password!", "Login incomplete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
passField.setText("");
}
}
break;
}
}
}
if (! userNameFound) {
int result = JOptionPane.showConfirmDialog(null, "Incorrect password!", "Login incomplete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
passField.setText("");
}
}