I’ve created a user login dialog box that compares the user’s name and password that they have stored in a csv file to what they have typed within corresponding textfields. Before the login appears, I’ve used JOptionPanes to display the return statements that I’ve passed the username name password into. The JOptionPane displays the name and password. But after I login appears, then enter the name and password to compare to, login is not successful. When I used JOptionPane to display the same username and password again, it says both are null. I’ve narrowed down the problem where I’ve setup a seperate boolean method that I use to see if what the user enters is equal to the return method’s value are, but for some reason, my return methods returns to NULL before comparing.
Any suggestions as to why this is happening? Again, for some reason, I’m losing the return values of “getName()” and “getPass().”
/**Read csv data. put data into array, then elements into variables */
String lines = "";
String unparsedFile = "";
FileReader fr = new FileReader("c:\\foo\\user.dat");
BufferedReader br = new BufferedReader(fr);
while((lines = br.readLine()) != null){
unparsedFile += lines;
}
br.close();
userData = unparsedFile.split(",");
String g = userData[0];
g1 = userData[0]; // return in String method "getName()"
this.name = g1;
h1 = userData[1]; // returned in String method "getPass()"
this.password = h1;
///////////////////////////////
/* check if what you entered is same as getName() and getPass() */
public static boolean authenticate(String username, String password){
String g2, h2;
g2 = cn.getName();
h2 = cn.getPass();
JOptionPane.showInputDialog(null, cn.getName() + " login " + cn.getPass());
if(username.equals(g2) && password.equals(h2)){
JOptionPane.showInputDialog(null, "True");
return true;
}
JOptionPane.showInputDialog(null, cn.getName() + " login false " + cn.getPass());
return false;
}
////////////////////////
/* Login: enter name and password, the check if it matches getName() and getPass() */
nam = tfUsername.getText();
passA = pfPassword.getText();
if(Login.authenticate(nam, passA)){
JOptionPane.showMessageDialog(UserLogin.this,
"Welcome, " + cn.getChatName() + ". Login successful.",
"Login",
JOptionPane.INFORMATION_MESSAGE);
succeeded = true;
dispose();
}
if(!nam.equals(cn.getName()) && !passA.equals(cn.getPass())){
JOptionPane.showMessageDialog(UserLogin.this, "Invalid username or password",
"Login unsuccessful", JOptionPane.ERROR_MESSAGE);
//reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;
}
}
});
Given that the following works fine
You have not provided enough information, we need the set code, update code because it is almost definately there the issue is (is cn the instance you think it is for example or has it been overwritten)
as it stands I cannot help further until you provide a shorter example.