1

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");
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
godopetza
  • 67
  • 1
  • 8
  • hello @xerx593 this doesnt work either still gives me the same output as before. all first lines are still read and gives user not found errror still. – godopetza Nov 27 '21 at 15:13

1 Answers1

0

My comment as code, with little re-thinking:

...
  scan.useDelimiter("[,\n]");
  boolean found = false; // Memorize...    
  while (!found && scan.hasNext()) { // ...process until found or end ("until(x or y)" <=> "while (not x and not y)" <==> "while not(x or y)";)
    String user = scan.next();
    String pass = scan.next();            
    if (username.equals(user.trim()) && password.equals(pass.trim())) {
      found = true; // .. don't break...
    } 
  }
  // show dialog (and do "the action") only after "processing database" ...
  if (found) {
    // log.info("yay");
    JOptionPane.showMessageDialog(null,"Welcome!"+username);
    new peopleoptions().setVisible(true);
  } else { // ... AND not found!;)
    JOptionPane.showMessageDialog(null,"Credentials invalid, please register new user or reset password");                    
  }
  // really??: this.dispose(); // in any case?? maybe: "finally"!
}  catch( ...

With the problem at hand, it comes down to:

  • looping trough the file, setting a boolean flag
  • then afterwards doing the correct stuff.

And please "free" your resources (File, Scanner, ...)!!

How to Correctly Close Resources

With java >= 8:

Try-With-Resources

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • Don't tell newbies to free Scanner. They'll close scanner and in passing, close `System.in` which you shouldn't be closing. Also, using `,` as delimiter means passwords cannot contain `,` - that's bad, and if the file is windows file endings, __it won't work__, as all passwords in the file are read in with a trailing `\r`. The regex `\\R` will do what you want, though. (That's regexpese for: A line ending, matches and consumes `\r\n` properly). – rzwitserloot Nov 27 '21 at 14:09
  • @rzwitserloot no, doubt! many findings!! I better delete my post, ...before [these guys](https://stackoverflow.com/tags/swing/topusers) (also) crush this!!! xDxD (copy quick, @OP)) – xerx593 Nov 27 '21 at 14:17
  • i actually also did not: "tell newbies to free Scanner" ...maybe "implicitely" ( for very "quick & curious" newbies), but *i see* a "leaking File" in the OP. – xerx593 Nov 27 '21 at 14:21