I am making a simple program that asks for a password before you can start the program. When my user inputs the wrong password, they get an "Access Denied," warning. I achieve this using an If/Else Statement. What I want to be able to do is to re-run my program if they enter the wrong password, as they cannot type in the console again if they get it wrong.
Here is my workspace:
import java.util.Scanner;
public class PasswordProtected {
public static void main (String args[]){
Scanner Password = new Scanner (System.in);
String mainpassword, userInput;
mainpassword = ("Jacob");
System.out.println("Please enter the password to continue.");
userInput = Password.nextLine();
System.out.println("Verifying Password");
if (userInput.equals(mainpassword)){
System.out.println("Access Granted");
System.out.println("Welcome!");
}else{
System.out.println("Access Denied");
}
}
}
I do realize that I could copy some thing like this over and over again, however, it is a waste of space is is not unlimited.
System.out.println("Please enter the password to continue.");
userInput = Password.nextLine();
System.out.println("Verifying Password");
if (userInput.equals(mainpassword)){
System.out.println("Access Granted");
System.out.println("Welcome!");
}else{
System.out.println("Access Denied");
}
}
Please note that I am new at programming, and may need a bit of extra help.
If the Else Statement is triggered, how can I fully restart my program without the use of manually clicking the run button again?