Skip to content
Advertisement

How do I make my code loop back to the beginning in Java?

I’ve read that I would need to put “continue” after an “if” statement, but every I tried this with my if statement and it states that “continue cannot be used outside of a loop.”

Advertisement

Answer

Set it in the loop. EX:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (true){
            System.out.print("Enter a password: The password must have at least eight characters, only letters and digits, and at least two digits. ");
            String s = input.nextLine();
            if (thepassword(s)) {
                System.out.println("Valid Password");
                break;
            } else {
                System.out.println("Invalid Password");
            }
        }
    }

See more : Java Break and Continute

Advertisement