Vivid
Vivid

Reputation: 37

How do I ask the user when he/she wants to quit the program

Here's the program that I'm working on but I'm having problems with figuring out how to ask the user if they are done with it by saying a word or number.

import java.util.Scanner;

class paliindrome {
    public static void main(String args[]) {

        String isPalindrome, reverse = "";
        Scanner in = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);
        PalindromeChecker aGame = new PalindromeChecker();

        System.out.println(" Please type a word and I'll figure out if it's a palindrome(The program is case sensitive).");
        isPalindrome = in.nextLine();


        int length = isPalindrome.length();

        for ( int i = length - 1; i >= 0; i-- )
            reverse = reverse + isPalindrome.charAt(i);

        if (isPalindrome.equals(reverse))
            System.out.println("The word that you have entered is a palindrome.");
        else
            System.out.println("The word that you have typed isn't a palindrome.");

        char answer;
        do {
            aGame.play ();

            System.out.print("\n Do you want to continue (y or n)?");
            answer = keyboard.next().charAt(0);
        } while (answer == 'y' || answer == 'Y');
    }
}

Upvotes: 2

Views: 1549

Answers (4)

cloudream
cloudream

Reputation: 16

import java.util.Scanner;

public class paliindrome {
    public static void main(String[] args) {

        String isPalindrome, reverse = "";
        Scanner in = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);
        PalindromeChecker aGame = new PalindromeChecker();
        for (;;) {// or while(true)
            System.out.println(
                    " Please type a word and I'll figure out if it's a palindrome(The program is case sensitive).");
            isPalindrome = in.nextLine();
            int length = isPalindrome.length();

            for (int i = length - 1; i >= 0; i--) {
                reverse = reverse + isPalindrome.charAt(i);
            }

            if (isPalindrome.equals(reverse))
                System.out.println("The word that you have entered is a palindrome.");
            else
                System.out.println("The word that you have typed isn't a palindrome.");

            System.out.print("\n Do you want to continue (y or n)?");
            char answer = keyboard.next().charAt(0);
            if (answer != 'y' && answer != 'Y') {
                break;
            }else{
                aGame.play ();
            }

        }

    }
}

Upvotes: 0

Rahul Kale
Rahul Kale

Reputation: 227

You can use below code to ask user for quit.

boolean quit = false;
        Scanner scanner = null;
        try {
            do {
                System.out.println("Do you want to quit? (true/false)\n");
                scanner = new Scanner(System.in);
                String input = scanner.nextLine();
                quit = Boolean.parseBoolean(input);
            } while (!quit);
        } finally {
            scanner.close();
        }

Upvotes: 0

Ashish Ani
Ashish Ani

Reputation: 332

There is one more way to solve your problem: Put your entire code in a infinite while loop like this while(true), on the last line of loop just before exiting from the loop, u can ask user if he/she wants to continue. If answer is yes, then use the code System.exit(0) to exit from the program or if you want to just break from the loop use break; in the if condition.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201467

Put your entire application within the loop. One easy way would be to default answer to y (and you can use Character.toUpperCase(char) to eliminate the or) and something like

PalindromeChecker aGame = new PalindromeChecker();
char answer = 'y';
while (Character.toUpperCase(answer) == 'Y') {
    String isPalindrome, reverse = "";
    Scanner in = new Scanner(System.in);
    Scanner keyboard = new Scanner(System.in);
    // ... The rest of your code ...
    aGame.play();

    System.out.print("\n Do you want to continue (y or n)?");
    answer = keyboard.next().charAt(0);
}

Upvotes: 1

Related Questions