I can't loop my code whenever condition fails. It just prints both blocks of code if the user input is true

I am a beginner at java and I want to loop this all over again but I don't know how. I've tried a while loop but it doesn't work really well and it prints both blocks of code. What should happen is when I type "quit, Quit, or QUIT", it should terminate. What happens instead is that it also prints the message "failed to terminate the program". What should I do? I've also tried an if statement which works fine but I don't know how to loop it if the condition fails.

import java.util.Scanner;

public class fortytwo {

    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("Hi there!");
        String quit = scanner.next();
                
        while (quit.equals("quit") || quit.equals("QUIT") || quit.equals("Quit")) {
            System.out.println("You terminated the program");
            break;
        } 
        System.out.println("You failed to terminate the program.\n To quit, type (quit), (Quit), or (QUIT)");
            
        scanner.close();
    }
}

Solution 1:

You are using a loop without a need for it. Also break only exits the loop, but continues execution after the loop. Replace the while with an if/else:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Hi there!");
    String quit = scanner.next();
            
    if(quit.toLowerCase().equals("quit")) {
        System.out.println("You terminated the program");
    } else {
        System.out.println("You failed to terminate the program.\n To quit, type (quit), (Quit), or (QUIT)");
    }
    scanner.close();
}

This does not prompt for input again which your second output hints at, but neither does your code.