0

This is my code.

import java.util.Scanner;
public class passwordProgram {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    
    String correctPassword = "WooHoo";
    int tries = 0;
    boolean keepGoing = true;
    
    while(keepGoing = true) {
        
        
        tries = tries + 1;
        System.out.println("try #" + tries);
        
        String password;
        
        System.out.println("Please enter the password: ");
        
        password = scan.next();
        
        if(password == correctPassword) {
            System.out.println("This is the correct result:" + password);
            keepGoing = false;
            
            if(tries >= 3) {
                System.out.println("Too many wrong tries. Exiting program");
                keepGoing = false;
                break;
            }   
        
    }

    }
}
}

The while loop doesn't end when the right password is entered, and it keeps repeating after the allowed number of attempts has been reached and I want to know why.

Is it because of the condition statement in the while loop or is there something else wrong with the code?

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • 2
    `while(keepGoing = true) ` should be `while(keepGoing == true) `. `=` is for assignment, `==` is for comparison. You can avoid the problem entirely by writing `while(keepGoing)`. – Federico klez Culloca Mar 25 '21 at 08:34
  • In addition with @FedericoklezCulloca you try to print "Too many wrong tries. Exiting program" if password is correctPassword. It should be in `else` part of `if(password == correctPassword)' – Dhay Mar 25 '21 at 08:45
  • There's no need to write "Answered" in the title. Instead mark an answer as the correct one or (if none fits) write an answer yourself and mark that instead. – Federico klez Culloca Mar 25 '21 at 08:58

4 Answers4

1
while (keepGoing = true)

this doesn't verify whether keepGoing is true, it sets it to true, so it remains true.

You should change it to:

while (keepGoing == true)

or, shorter:

while (keepGoing)

EDIT: Another problem you have, is the way you compare your String values.

if(password == correctPassword)

The == operator is used to compare references of Objects, or primitive values, not the values of Objects.

What you want here, is:

if ( correctPassword.equals(password))

Here's a good read about that: How do I compare strings in Java?

EDIT 2: Your conditional statements shouldn't be nested. If they are, that means the second one will only execute if the first one evaluates to true:

    if(correctPassword.equals(password)) { // already corrected
        System.out.println("This is the correct result:" + password);
        keepGoing = false;
        
        if(tries >= 3) {
            System.out.println("Too many wrong tries. Exiting program");
            keepGoing = false;
            break;
        }          
}

should be rewritten as:

if(correctPassword.equals(password)) { // already corrected
    System.out.println("This is the correct result:" + password);
    keepGoing = false;
}
    
    if(tries >= 3) {
        System.out.println("Too many wrong tries. Exiting program");
        keepGoing = false;
        break;
    }    

  
Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • I have used that before but it seems not to work. I wanted the loop to break after 3 tries or when i get the correct password. Ill try again and see it its corrected the problem thatnks. – yuricaptinprice Mar 25 '21 at 08:45
  • Could it be because of the 2 if statements? – yuricaptinprice Mar 25 '21 at 08:46
  • @yuricaptinprice you have a second major error, I'll update my answer – Stultuske Mar 25 '21 at 08:46
  • Ok ive gotten that corrected but it still loops, is it because of the two if statments and me not implementing a break after each one? Edit: I got it corrected. Seems like the 2 if stements were together so even if I inputted the correct value it would still keep going. Thank you for your help – yuricaptinprice Mar 25 '21 at 08:52
  • @yuricaptinprice your two conditional statements are nested, so the second will only be executed if the first evaluates to true. you'll need to change the scope of the first, by replacing the } – Stultuske Mar 25 '21 at 08:56
0

Your condition is an assignment, not a check.

Try changing it to while(keepGoing==true).

Dimitris Sfounis
  • 2,400
  • 4
  • 31
  • 46
0

The '=' operator sets a value to a variable. The '==' operator compares values. A good practice it would be to just write the following statement:

while(keepGoing) {
  ...
}

You can just parse a boolean inside the 'while' statement and it will go on while the boolean is true.

Stoyanov
  • 61
  • 5
0

In order to check a condition you should use "==" instead of "=" so the while statement should look like this:

    while(keepGoing == true) {
    ...
    }