Java 不确定密码检查器的逻辑错误在哪里
因此,对于我的家庭作业,我们需要做一个密码检查器,用户可以使用它登录到程序,并说明他们使用的是有效的还是无效的凭据 因此,我尝试将它与我所有的密码变体进行比较,甚至尝试将它与文件中密码的确切字符串进行比较,但它似乎仍然不起作用,而且我似乎无法找到我的逻辑在其中失败的地方 我的身份验证程序,用户在其中输入用户名和密码:Java 不确定密码检查器的逻辑错误在哪里,java,logic,Java,Logic,因此,对于我的家庭作业,我们需要做一个密码检查器,用户可以使用它登录到程序,并说明他们使用的是有效的还是无效的凭据 因此,我尝试将它与我所有的密码变体进行比较,甚至尝试将它与文件中密码的确切字符串进行比较,但它似乎仍然不起作用,而且我似乎无法找到我的逻辑在其中失败的地方 我的身份验证程序,用户在其中输入用户名和密码: import javax.swing.*; import java.awt.event.*; import java.awt.*; class Authenticator ext
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
import java.util.*;
导入java.io.*;
类密码检查器{
private ArrayList user=new ArrayList();
private ArrayList pass=new ArrayList();
//构造函数:加载包含文件中的内容的列表
公共密码检查器(){
字符串filename=“Logins.txt”;
扫描仪文件输入;
试一试{
fileIn=newscanner(newfilereader(filename));
while(fileIn.hasNext()){
user.add(fileIn.next());
pass.add(fileIn.next());
}
fileIn.close();
}
捕获(例外e){
System.out.println(e.getMessage());
}//终点接住
}//结束构造函数
//获取密码
公共字符串getPassword(字符串用户名){
对于(int i=0;i
我希望我的密码检查发送一个正确或错误的声明,我的验证者输入他们是否有正确的细节
在我的Logins.txt中,我的用户名和密码与此类似。
用户名密码
123 123在Java中,字符串比较是通过方法
string.equals来完成的
在if(pWord==password)
行中,您使用了相等运算符=
,该运算符仅检查两个引用是否指向同一对象。
我会将if(pWord==password)
更改为if(pWord.equals(password))
注意
Java对字符串使用缓存系统,因此有时使用=
可能会得到正确的结果,但情况并非总是如此。哇,我不敢相信我忽略了这一点。非常感谢。把它清理干净,解决了那个问题。有时,一双新的眼睛会有所帮助(也有人知道得更多)