Im making a login using java. The authentication keeps on looping and displaying error message until it gets the username and password right. How to fix it? I only want it to break if found and only show not found when not found.
private void loginbtnActionPerformed(java.awt.event.ActionEvent evt) {
String username = usertxt.getText();
String password = passwordtxt.getText();
try
{
File login = new File("logindata.txt");
Scanner scan = new Scanner(login);
scan.useDelimiter("[,\n]");
while(scan.hasNext())
{
String user = scan.next();
String pass = scan.next();
if (username.equals(user.trim()) && password.equals(pass.trim()))
{
JOptionPane.showMessageDialog(null,"Welcome!"+username);
this.dispose();
new peopleoptions().setVisible(true);
break;
}
else
{
JOptionPane.showMessageDialog(null,"User not found, please register");
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"System Error");
}
}