[英]How can I compare User and password input to a text file?
Heyoo!嘿哟! I'm creating a simple login form using the swing package in java and I am having trouble in checking if the username and password is correct or not.
我正在使用 java 中的 swing 包创建一个简单的登录表单,但在检查用户名和密码是否正确时遇到了麻烦。
this here is the code currently:这是当前的代码:
public void actionPerformed(ActionEvent e) {
try{
File user = new File("Usernames.txt");
File pass = new File("Passwords.txt");
FileReader frUsername = new FileReader(user);
FileReader frPassword = new FileReader(pass);
BufferedReader brUsername = new BufferedReader(frUsername);
BufferedReader brPassword = new BufferedReader(frPassword);
String username = brUsername.readLine();
String password = brPassword.readLine();
if (e.getSource() == btnLogin){
while(username != null && password != null){
if ((txtUsername.getText()).equals(username) && (new String (jpfPassword.getPassword()).equals(password))){
JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful",JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
}
else{
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login",0); //this is for testing purposes only
}
break;
}
}
brUsername.close();
brPassword.close();
}
catch(IOException err){
System.err.println("File not found.");
}
}
} }
The idea is to have multiple accounts stored in the password and usernames files.这个想法是将多个帐户存储在密码和用户名文件中。 for example the file content is:
例如文件内容是:
Username.txt:用户名.txt:
SampleUsername1
SampleUsername2
Password.txt:密码.txt:
SamplePassword1
SamplePassword2
If line 1 from the username file is "sampleUsername1" then the password for it is also from line 1 "samplePassword1" of the password file.如果用户名文件的第 1 行是“sampleUsername1”,那么它的密码也来自密码文件的第 1 行“samplePassword1”。 if the user and password isn't the same line or not in the file, it should give an "invalid" error.
如果用户和密码不在同一行或不在文件中,则应给出“无效”错误。 I know it is not secure to put passwords in a txt file but this is only for practice purposes as I am still learning how to code.
我知道将密码放在 txt 文件中并不安全,但这仅用于练习目的,因为我仍在学习如何编码。 Any kind of help and tips is really appreciated.
非常感谢任何形式的帮助和提示。 Thanks!
谢谢!
This works fine for me:这对我来说很好:
public static void main(String[] args) {
String txtUsername = "username2";
String jpfPassword = "password2";
try {
File user = new File("Usernames.txt");
File pass = new File("Passwords.txt");
FileReader frUsername = new FileReader(user);
FileReader frPassword = new FileReader(pass);
BufferedReader brUsername = new BufferedReader(frUsername);
BufferedReader brPassword = new BufferedReader(frPassword);
String username = brUsername.readLine();
String password = brPassword.readLine();
boolean loginSuccess = false;
while (username != null && password != null) {
if ((txtUsername).equals(username) && (jpfPassword.equals(password))) {
JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful", JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
loginSuccess = true;
break;
}
username = brUsername.readLine();
password = brPassword.readLine();
}
if (!loginSuccess) {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login", 0); //this is for testing purposes only
}
brUsername.close();
brPassword.close();
} catch (IOException e) {
e.printStackTrace();
}
}
My Usernames.txt
and Passwords.txt
look like this我的
Usernames.txt
和Passwords.txt
看起来像这样
username
username1
username2
respectively分别
password
password1
password2
The main problem was that you were only checking the first line.主要问题是您只检查第一行。 Once you changed the
break;
一旦你改变了
break;
to the readline()
methods, the and you can't give the failed message everytime you check a name.对于
readline()
方法,每次检查名称时都不能给出失败的消息。 That's why you have to loop through everything first, and then check if you failed or not.这就是为什么您必须先遍历所有内容,然后检查您是否失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.