【发布时间】:2019-11-08 04:11:06
【问题描述】:
因此,对于我的家庭作业,我们需要制作一个密码检查器,用户可以使用它来登录程序并说明他们使用的是有效凭据还是无效凭据。
所以我尝试将它与我所有的密码变体进行比较,甚至尝试将其与文件中密码的确切字符串进行比较,但它似乎仍然不起作用,我似乎无法找到我的逻辑在里面失败了。
我的身份验证程序,用户在其中输入用户名和密码。:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Authenticator extends JPanel{
//Windows Components
private JFrame window = new JFrame("Login");
private JLabel lblUser = new JLabel("Username:");
private JLabel lblPass = new JLabel("Password:");
private JLabel lblCorrect = new JLabel("Correct Login Credentials");
private JLabel lblWrong = new JLabel("Incorrect Login Credentials");
private JTextField txtUser = new JTextField();
private JPasswordField txtPass= new JPasswordField();
private JButton btnLogin = new JButton("Login");
//TODO: Create an instance of the PasswordChecker
private PasswordChecker check = new PasswordChecker();
//Constructor: Sets up the window
public Authenticator(){
//Absolute layout requires null
setLayout(null);
//Sets x, y, w, h of each component
lblUser.setBounds(10, 10, 200, 30);
lblPass.setBounds(10, 50, 200, 30);
txtPass.setBounds(120, 50, 200, 30);
txtUser.setBounds(120, 10, 200, 30);
lblCorrect.setBounds(10, 100, 200, 30);
lblWrong.setBounds(10, 100, 200, 30);
btnLogin.setBounds(220, 100, 100, 30);
//add components to panel
add(lblUser);
add(lblPass);
add(txtUser);
add(txtPass);
add(lblCorrect);
add(lblWrong);
add(btnLogin);
lblCorrect.setVisible(false);
lblWrong.setVisible(false);
//Adds panel to window
window.add(this);
//Closes the program when window closes
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 250);
window.setVisible(true);
btnLogin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String userName = txtUser.getText();
String passWord = txtUser.getText();
if(check.checkLogin(userName, passWord)){
lblCorrect.setVisible(true);
}//end if
else{
lblWrong.setVisible(true);
}//end else
}
});
}//Ends constructor
public static void main(String args[]){
new Authenticator();
}
}
这是我的密码检查器,用于检查用户输入,并将用户输入的内容与身份验证器进行比较:
import java.util.*;
import java.io.*;
class PasswordChecker{
private ArrayList<String> user = new ArrayList<>();
private ArrayList<String> pass = new ArrayList<>();
//Constructor: load the lists with the stuff from the file
public PasswordChecker(){
String filename = "Logins.txt";
Scanner fileIn;
try{
fileIn = new Scanner(new FileReader(filename));
while(fileIn.hasNext()){
user.add( fileIn.next() );
pass.add( fileIn.next() );
}
fileIn.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}//ends catch
}//ends constructor
//GetPassword
public String getPassword(String userName){
for(int i = 0; i < user.size(); i++){
if(user.get(i).equals(userName)){
String password = pass.get(i);
return password;
}//end if
}//end for
return userName;
}//end getPassword
//Check Login
public boolean checkLogin(String userName, String password){
for(String s: user){
String uName = s;
String pWord = getPassword(s);
if(pWord == password){
return true;
}//end if
}//end while
return false;
}//end checkLogin
}//ends Class
我希望我的密码检查器发送一个真假声明,让我的身份验证者输入他们是否有正确的详细信息。
在我的 Logins.txt 中,我的用户名和密码与此类似。 用户名密码 123 123
【问题讨论】: