1

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?

3
  • 4
    Have you heard of loops (for, while etc.)? Just loop over the code until the input is correct, and then continue the program (maybe put a maximum number of tries in). Commented Apr 22, 2015 at 19:55
  • I have forgot to sate that I am new at programming. I will edit my question accordingly. Commented Apr 22, 2015 at 19:57
  • or give them a enter password or exit option, but as @dunni says While(!Verified) { GetPassword()} is basically the way to go. Commented Apr 22, 2015 at 19:57

3 Answers 3

2

You don't need to restart your program. Use a loop to ask for the password again if it is incorrect. For example in semi-pseudo code with while statement:

userInput = input.nextLine();
while ( !userInput.equals(mainpassword) ){
    userInput = input.nextLine();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried everyones response, (Perhaps incorrectly) however, yours worked.
I have realized that I cannot get my user to input any other information after this. Why? Whenever I try to press enter (For the new info) the same code pops up about creating an account again.
forget it ;) It was getting way to confusing either way. Honestly, I dont even understand my own code. I will most likely rebuild from scratch, or ditch the project.
1

Try while(true) loop

String mainpassword = ("Jacob");
String userInput = null;
Scanner Password = new Scanner (System.in);

while(true) {
    userInput = Password.nextLine();

    if (userInput.equals(mainpassword)){
        break;
    } else {
        System.out.println("Access Denied");
    }
}

System.out.println("Access Granted");
System.out.println("Welcome!");

1 Comment

This. But better option is to analyze if their entry was blank and then do instead of a do...while(userInputWasNotBlank)... then instruct them that if they want to exit they just need to press enter without entering anything else... see docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
1

If you still feel to restart any Java program, then the main() method can be called from anywhere else in your code. You can just call this method, passing in any necessary String parameters. Use a thread to do so, as below

Thread t = new Thread() {
     public void run() {             
         String[] args = { };
         PasswordProtected.main(args);     
     }  
};  
t.start(); 

If you want to re-launch your application in a new process, you can use

Runtime.getRuntime().exec(...);

Comments

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.