2

Below is a simple program that I wrote that will ask for a password. If I enter the incorrect password I am prompted with "Password incorrect would you like to try again?", and if I say no or anything else that doesn't begin with a 'y', it will terminate the program. The problem is, if I enter the correct password which is "Noah" it says "Password correct" and it loops back to "Enter password" again. How can I make this program terminate after I enter the correct password? Thank you.

import java.util.Scanner;
public class methods
{
   public static void main (String [] args)
   {
      Scanner sc = new Scanner(System.in);
      String response = "yes";
      System.out.println("Enter password:");
      while(response.charAt(0)=='y')
      {
         String input = sc.nextLine();
         if(input.equalsIgnoreCase("Noah")==true)
         {
            System.out.println("Password correct");
         }
         else if(input.equalsIgnoreCase("Noah")==false)
         {
            System.out.println("Password incorrect, would you like to try again?");
            response = sc.nextLine();
         }
      }
   }
}
2
  • 3
    input.equalsIgnoreCase("Noah")==true Remove the ==true, I get angry when I see this. Commented Dec 30, 2013 at 7:39
  • also the "if(input.equalsIgnoreCase("Noah")==false)" is not needed, it if it's not true, its false. Commented Sep 15, 2014 at 23:25

5 Answers 5

2
  while(response.charAt(0)=='y')
      {  System.out.println("Enter password")
         String input = sc.nextLine();
         if(input.equalsIgnoreCase("Noah")==true)
         {
            System.out.println("Password correct");
            break;
         }
         else if(input.equalsIgnoreCase("Noah")==false)
         {
            System.out.println("Password incorrect, would you like to try again?");
            response = sc.nextLine();
         }
      }
Sign up to request clarification or add additional context in comments.

Comments

2

use break; then you can terminate.

if("Noah".equalsIgnoreCase(input)){
    System.out.println("Password correct");
    break;
}

Comments

2

There are a few ways. In increasing severity:

1) Use a break statement. That will take program control flow to just after the end of the while loop.

2) Use a return statement. That will exit the function and, in your specific case, that will end the program.

3) Insert System.exit(n) where n is a number, probably non-zero, to indicate a return status. This terminates the Java Virtual Machine, and returns the value n to the operating system.

In your case I'd be inclined to go with (1) or (2).

Comments

1

if(input.equalsIgnoreCase("Noah")==true) { System.out.println("Password correct"); break;
}

or you can add response ="no";

if(input.equalsIgnoreCase("Noah")==true) { System.out.println("Password correct"); response ="no";
}

" no" or anything that dose not start with the 'y' char .

Comments

0

After doing a couple of changes, I guess this is what you are looking for :

import java.util.Scanner;

public class Methods{

public static void main (String [] args){

    Scanner sc = new Scanner(System.in);
    String response = "yes";

    while(response.charAt(0)=='y'){
         System.out.println("Enter password:");
        String input = sc.nextLine();
        if(input.equalsIgnoreCase("Noah")==true){
            System.out.println("Password correct");
            break;
        }
        else if(input.equalsIgnoreCase("Noah")==false){
            System.out.println("Password incorrect, would you like to try again?");
            response = sc.nextLine();
        }
    }
}

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.