0

An exercise that I am stuck on is one using the do-while loop. I am supposed to prompt the user to enter the password, then re-enter the password again. If these passwords match then they will gain access. If the two passwords do not match then I am supposed to prompt the user to enter the password again 3 more times. After the third time it will tell them access is denied. I am having difficulty distinguishing what goes where in the do-while loop.

This is what I have (updated)...still confused as to why it is not terminating:

import java.util.Scanner;

public class PasswordChecker{

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);    

    String password1;    
    String password2;    

    int i = 1;

    do {

      System.out.println("Please enter your password");
      password1 = sc.nextLine();

      System.out.println("Please re-enter your password");
      password2 = sc.nextLine();

    } while(!password1.equals(password2) && i<=3);

    i++;

    }
}
confused
  • 15
  • 2
  • 5
  • 1
    http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – jmj Oct 08 '14 at 00:25
  • 4
    `while(i <= 3 && !password1.equals(password2));` – jmj Oct 08 '14 at 00:26
  • 2
    `i` is been incremented outside of the `do-while` loop, so it will never be incremented while your checking the password. You also never give the user an opportunity to re-enter the password within the `do-while` loop. Start by getting it work once (without the loop) and the wrap the loop around that, been sure to include the required exit logic for the loop – MadProgrammer Oct 08 '14 at 00:28
  • what would the correct exit logic be? I have been using if(i>3) then access is denied under the while portion. That hasn't worked. – confused Oct 08 '14 at 01:35

2 Answers2

1

Your = comparison is wrong. If you want to make sure two strings have the same value, you need to use the String equals method. '=' is an assignment operator if (a=b) {} doesn't compile, and '==' checks if two variables are pointing to the same thing. if (a==b) {} won't always succeed (see example below). The String equals method always looks at the actual value represented by a String.

String a = "1";
String b = "12".substring(0,1);
a == b // false
a.equals(b) // true

Now with that out of the way - there are lots of ways you can get the exit condition. If you want to do it all in one statement, you can change i<=3 in your while condition i++ <= 3. My preference is to separate the two conditions like this

do {
    ...
    if (password.equals(password2)) {
        break;
    }
} while (i++ <= 3);
user2910265
  • 844
  • 1
  • 9
  • 15
0

it never ends because you never increment the i counter, the i++; should be inside the do while statement. Try initializing i = 0 , and increment it at the begining of each operation.

import java.util.Scanner;

public class PasswordChecker{

    public static void main(String[] args) {

      Scanner sc = new Scanner(System.in);    

      String password1;    
      String password2;    

      int i = 0;

      do {
       i++;

       System.out.println("Please enter your password");
       password1 = sc.nextLine();

       System.out.println("Please re-enter your password");
       password2 = sc.nextLine();

     } while(!password1.equals(password2) && i<=3);
    }
}
Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49
kiwis
  • 1