0
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
    public static void main(String[] args) {
        System.out.println("What is the password?");
        Scanner new2 = new Scanner(System.in);
        int input = 0;

        while(input <= 5 )
        {
            String password = new2.nextLine();
            if(!password.equals("bluesky123")){
                System.out.println("Incorrect password");
                input++;
            }

            else if("bluesky123".equals(password)) {
                System.out.println("You got it right!");
                break;
            }
            else if(input == 5) {
                System.out.println("maximum number of attempts reached");
                break;
            }
        }    
    }

}

basically, once I hit the 5 loops, it just says "incorrect password" and breaks. not the "maximum attempts" message.

6
  • 1
    How do you now it doesn't start over? it loops right back and waits for another password to be entered. Enter wrong password 5 times, and then the program will terminate. Did you perhaps intend for the println("What is the password?") to be inside the loop? Commented Jan 15, 2017 at 21:28
  • Sorry, I just realized I asked the wrong question. Basically whenever I hit the 5th attempt, it still only outputs the "incorrect password" but breaks. Commented Jan 15, 2017 at 21:31
  • 2
    Your logic is also suspect. If I'm reading it right, you'll never enter the code block for input == 5. You cover all cases with the first 2 blocks, and with either exit on the next loop where input is 5 or more (enter !password.equals raises the variable to 5), or break because you got the right password. Commented Jan 15, 2017 at 21:31
  • It doesn't "break". The loop ends, because the loop condition (input <= 5) is false. Your code doesn't work because if !password.equals("bluesky123") is false and first if statement skips, then "bluesky123".equals(password) is definitely true, so there is no way for second if statement to skip, ergo there is no path thru the code that you ever allow else if(input == 5) to execute. Commented Jan 15, 2017 at 21:33
  • Suggestion: move the verification of input == 5 to the first case, and have the String verifications come after. Commented Jan 15, 2017 at 21:41

3 Answers 3

3

Allow me to annotate:

This if statement will always be evaluated:

        if(!password.equals("bluesky123")){
            System.out.println("Incorrect password");
            input++;
        }

This if statement will only be evaluated if the password is "bluesky123". In this case, it will always evaluate to true.

        else if("bluesky123".equals(password)) {
            System.out.println("You got it right!");
            break;
        }

There is no case when this if statement will ever be evaluated. Once if-else finds a statement that is true, it will skip all others in that section.

        else if(input == 5) {
            System.out.println("maximum number of attempts reached");
            break;
        }

In your case, you should consider a nested if (i.e. an if inside another if).

Sign up to request clarification or add additional context in comments.

Comments

1
while(input <= 5 )
    {
        String password = new2.nextLine();
        if(!password.equals("bluesky123")){
            System.out.println("Incorrect password");
            input++;
        }
        else {
            System.out.println("You got it right!");
            break;
        }

        if((input == 5) && (!password.equals("bluesky123"))) {
            System.out.println("maximum number of attempts reached");
            break;
        }
    }    

Your logic has some flaws. You have to pay attention to how JAVA processes if / else if

https://www.tutorialspoint.com/java/if_else_statement_in_java.htm

Comments

1

I tested your code it is working! The only thing that you need to do is to move the follow line to inside the while loop

 System.out.println("What is the password?");

Doing this it will print "Incorrect password" and then it will print again "What is the password?"

Because in the way that it is working now seems that the software is not waiting the password to be retyped when in fact it is.

1 Comment

This answer unfortunately no longer applies, because OP changed the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.