#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Checking to see if password exists


    So, I have got my code working up until this point. I have a while loop in my test driver to ask the user to for the password if it already exists in the password file. It runs and it will check it, but once you enter the loop I cannot exit it even if the password is something new. I've checked the logic in that loop and the already exists method. Maybe I'm thinking of it backwards, but I'm not sure how to fix it.

    This is the already exists method:
    Code:
    public boolean passwordAlreadyExists() throws IOException
        {
        
            System.out.println("DEBUG: passwordAlreadyExists for given password=" + workingPassword );
            int current = 1,  matchesFound = 0;
            String eachLine;
            scan = new Scanner(passwordFile);
            while(current <= getNumberOfPasswordsToRemember())
            {
                eachLine = scan.nextLine();
                  if(eachLine.equals(workingPassword))  matchesFound++;
                  if(current == 1) password1 = eachLine;
                  else if(current == 2) password2 = eachLine;
                  else if(current == 3) password3 = eachLine;
                  else if(current == 4) password4 = eachLine;
                 current++;
            }
            if(matchesFound == 0) return false;
            else return true;
            
          }
    And this is the loop in the test driver that calls the method:
    Code:
      // check to see if the password already exists
           while(p.passwordAlreadyExists() != false)
           {
             workingPassword = JOptionPane.showInputDialog(null, 
                               "I'm sorry that password already exists, \n" +
                               "Please enter a password that has not been used, " +
                               p.getNumberOfPasswordsToRemember()+ "previous times." , 
                               JOptionPane.QUESTION_MESSAGE);
           } 
    
           
           // then we will write it to the file, which opens the file, write it, and then closes the file. 
           p.writePasswordToFile();
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    once you enter the loop I cannot exit it
    What is the value returned by: getNumberOfPasswordsToRemember()
    Does the value of current increase as expected and eventually become greater?

    Code:
    while(p.passwordAlreadyExists() != false)
    Is that an obscure way of testing for true?
    When does the value returned by that method change?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    What is the value returned by: getNumberOfPasswordsToRemember()
    Does the value of current increase as expected and eventually become greater?

    Code:
    while(p.passwordAlreadyExists() != false)
    Is that an obscure way of testing for true?
    When does the value returned by that method change?
    The value returned should be a boolean true or false. The matches found should increase if it is in the file.

    And the value returned from passwordAlreadyExists changes when the matchesFound is more than 0, or really 1.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    When does the value returned by that method change?
    What that was meant to do was to make you think about when of if the value returned by the method changed. If the value it returns never changes, the while() loop will execute forever.

IMN logo majestic logo threadwatch logo seochat tools logo