0

The loop is working fine except no matter what you put into txtUsername and txtPassword and I click the Login button, I get the message box saying invalid password try again. It is not crashing, just saying the password and username are always incorrect. Any help would be greatly appreciated!

    btnLogin.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            File loginF = new File("users.txt");

            //userTextField.getText();
            //pwBox.getText();
            //Scanner fileScan = null;

            try {
                Scanner fileScan = new Scanner(loginF);
                fileScan = new Scanner(new File("users.txt"));
                fileScan.useDelimiter(",");

            boolean found = false; 

            while (fileScan.hasNextLine()) {
                String input = fileScan.nextLine();
                String Username = input.substring(0, input.indexOf(' '));
                String Password = input.substring(input.indexOf(' '), input.length());

                if (Username.equals(userTextField) || (Password.equals(pwBox))) {
                    found = true; // added this to set found
                    System.out.println("Success!");
                }

                if (!found) {
                    JOptionPane.showMessageDialog(null,
                        "Invalid password. Try again.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                    }
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ProjectLogin.class.getName()).log(Level.SEVERE, null, ex);
            }  
        }

   });
MarkM
  • 1
  • 1
  • 1
    Please use formatting tools to properly edit and format your question/answer. Codes within sentences are to be formatted as `code` Very Important words to be **bold** , lesser important onces *Italic* – Morse Apr 04 '18 at 15:53
  • check this out - https://stackoverflow.com/questions/28766377/how-do-i-use-a-delimiter-in-java-scanner – Mauricio Gracia Gutierrez Apr 06 '18 at 13:42

2 Answers2

0

I believe the problem is that your error message logic is held within the loop. Try pulling it out of the loop, or rework the logic. Move the below code.

             if (!found) {
                JOptionPane.showMessageDialog(null,
                    "Invalid password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
             }
0

Here is my aproach

  1. check that user AND password match (change OR to AND)
  2. stop loop when a match is found
  3. check if found after loop ends

    try 
    {
        Scanner fileScan = new Scanner(loginF);
        fileScan = new Scanner(new File("users.txt"));
        fileScan.useDelimiter(",");
    
        boolean found = false; 
    
        while (!found && fileScan.hasNextLine()) 
        {
            String input = fileScan.nextLine();
            String Username = input.substring(0, input.indexOf(' '));
            String Password = input.substring(input.indexOf(' '),   input.length());
    
            //user AND password have to match 
            found = (Username.equals(userTextField) &&  (Password.equals(pwBox))) ;
    
        }
    
        if(found)
        {
            //You should redirect to the lading page..instead of this
            System.out.println("Success!");
        }
        else 
        {
            JOptionPane.showMessageDialog(null,
                "Invalid password. Try again.",
                "Error Message",
                JOptionPane.ERROR_MESSAGE);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ProjectLogin.class.getName()).log(Level.SEVERE,    null, ex);
    }  
    
  • Thank you very much for your response! I am still having some issues with this code. Now, it is saying there is a match each time no matter what you put into the username and password textfields. I have tried adjusting where I think appropriate but still no success. Any idea how you would go about this? Thanks again! – MarkM Apr 06 '18 at 09:36
  • @MarkM the userTextField and pwBox are being assigned or modified after they were input ? the file has the correct format ? are you seening any exception ? – Mauricio Gracia Gutierrez Apr 06 '18 at 13:28
  • so the file is separated by ", and also has new lines, and between user and password there is a space ? that sounds weird, maybe that is causing your problems – Mauricio Gracia Gutierrez Apr 06 '18 at 13:31
  • So the file it is reading from goes exactly like this: mark,1234 sean,2345. Each username and password combination are on a new separate line. Is this the problem? – MarkM Apr 06 '18 at 14:45
  • since user and pass are separated by "," why are you using indexOf(' ') ? try usin fileScan.next() to obtain username and password (since you already configure it to use "," as a field separator) – Mauricio Gracia Gutierrez Apr 06 '18 at 15:02