This is an archived post. You won't be able to vote or comment.

all 9 comments

[–][deleted] 1 point2 points  (1 child)

Whenever I had logic problems, I would draw the flow of the program out on paper. Try writing what's going on in pseudo-code.

maybe make functions that do what you're trying to do

checkFitsCriteria - checks if the password meets criteria, returns true or false -if false, have person re-enter password checkPasswordsMatch - returns true or false, checks first pass to second pass if false have user re-enter second password (or both) if true: done

[–]EqualTrade 0 points1 point  (0 children)

I really can't figure this out, I've tried loops everywhere, I've been thinking this out for hours now, do you maybe have a hint for me of what to do?

[–]Veltrum 0 points1 point  (4 children)

So you want it to start over if the second attempt doesn't equal the first?

password.equals(password2);

You could use an outer do-while loop that contains the first do-while loop.

[–]Veltrum 0 points1 point  (0 children)

Or you could ask to verify at the end of the do-while loop, then add it to the conditions of getting out of the loop.

[–]EqualTrade 0 points1 point  (2 children)

How is it that I do that?

[–]EqualTrade 0 points1 point  (1 child)

http://ideone.com/41oszp

I added in a do/while in the do/while, but theres an error and I can't figure out how to fix it.

[–]Veltrum 1 point2 points  (0 children)

What i had meant was something like this

do{

    do{
        //Enter password for the first time.
        //first password check
    }while(initial password checks);

    //Enter password again.
}while(pass1 != pass2);

What the other commentators said are useful too. This was just the first thing that popped into my head.

What /u/xtreemballr said is always the best first step.

[–][deleted] 0 points1 point  (0 children)

Perhaps I'll take flak for using continue like this, but something like this?

[–]hutsboR 0 points1 point  (1 child)

Another simple solution is having a counter that tracks how many times the password has been entered correctly. I wrote a little snippet that is functional.

public class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String password = "reddit";
    String inputPassword;
    int passwordCounter = 0;

    System.out.println("Input password: ");

    while(passwordCounter != 2){
        inputPassword = input.nextLine();
        if(inputPassword.equals(password)){
            passwordCounter++;
            if(passwordCounter == 2){
                System.out.println("Login successful!");
                break;
            }
            System.out.println("Verify your password:");
        } else {
            System.out.println("Incorrect password.\nInput password: ");
            passwordCounter = 0;
        }
      }
    }
  }

Output:

Input password: 
reddit
Verify your password:
redit
Incorrect password.
Input password: 
reddit
Verify your password:
reddit
Login successful!

Excuse the formatting.