user1712544
user1712544

Reputation:

How to make a program restart if wrong input in java

In java if the user puts in the wrong input how do you make the program restart at the question you asked them for example in the code that i posted below i got the question Pick a number between 0-9 how do i make it restart at that question if the user inputs a wrong input like 10 or higher or a string. I would greatly appreciate any tips or suggestions.

import java.util.Scanner;

public class Project2 {

    public static void main(String[] args) {
        String PlayAgain = "Y";

        while (PlayAgain.equals("Y")) {
            // Generate a secret Numbers
            int number1 = (int) (Math.random() * 10);
            int number2 = (int) (Math.random() * 10);
            int number3 = (int) (Math.random() * 10);
            int arr[] = new int[3];
            arr[0] = number1;
            arr[1] = number2;
            arr[2] = number3;

            boolean g1;
            boolean g2;
            boolean g3;

            g1 = false;
            g2 = false;
            g3 = false;

            // Show secret numbers
            System.out.println(arr[0] + ", " + arr[1] + ", " + arr[2]);

            Scanner input = new Scanner(System.in);

            // Prompt the user to enter a guess

            System.out.print("Enter your lottery pick (0-9): ");
            int guess1 = input.nextInt();

            if (guess1 > 9) {
                System.out
                        .println("Wrong input enter only 1 number between 0-9: ");
            } else {

            }

            System.out.print("Enter your lottery pick (0-9): ");
            int guess2 = input.nextInt();

            if (guess2 > 9) {
                System.out
                        .println("Wrong input enter only 1 number between 0-9: ");
            } else {

            }

            System.out.print("Enter your lottery pick (0-9): ");
            int guess3 = input.nextInt();

            if (guess3 > 9) {
                System.out
                        .println("Wrong input enter only 1 number between 0-9: ");
            } else {
                continue;

            }

            // Show secret numbers and your numbers
            System.out.println("The Secret Numbers are " + arr[0] + (", ")
                    + arr[1] + (", ") + arr[2]);
            System.out.println("Your Numbers are " + guess1 + (", ") + guess2
                    + (", ") + guess3);

            if (guess1 == arr[0] && guess2 == arr[1] && guess3 == arr[2]) {
                System.out
                        .println("Three Matching in Exact order, You Won $100,000 ");
            } else if ((

            (guess1 == arr[1] || guess1 == arr[2])
                    && (guess2 == arr[0] || guess2 == arr[2]) && (guess3 == arr[0] || guess3 == arr[1]))

                    ||

                    ((guess1 == arr[0])
                            && (guess2 == arr[1] || guess2 == arr[2]) && (guess3 == arr[1] || guess3 == arr[2]))

                    ||

                    ((guess2 == arr[1])
                            && (guess3 == arr[0] || guess3 == arr[1]) && (guess1 == arr[0] || guess1 == arr[2]))

                    ||

                    ((guess3 == arr[2])
                            && (guess2 == arr[0] || guess2 == arr[2]) && (guess1 == arr[1] || guess1 == arr[0])))

            {
                System.out
                        .println("Three Matching in Different order, You Won $10,000 ");
            } else if (

            ((guess1 == arr[0] || guess1 == arr[1] || guess1 == arr[2]) && (guess2 == arr[0]
                    || guess2 == arr[1] || guess2 == arr[2]))
                    && (guess3 != arr[0] || guess3 != arr[1] || guess3 != arr[2])

                    ||

                    ((guess1 == arr[0] || guess1 == arr[1] || guess1 == arr[2]) && (guess3 == arr[0]
                            || guess3 == arr[1] || guess3 == arr[2]))
                    && (guess2 != arr[0] || guess2 != arr[1] || guess2 != arr[2])

                    ||

                    ((guess2 == arr[0] || guess2 == arr[1] || guess2 == arr[2]) && (guess3 == arr[0]
                            || guess3 == arr[1] || guess3 == arr[2]))
                    && (guess1 != arr[0] || guess1 != arr[1] || guess1 != arr[2])

            ) {
                System.out.println("Any Two Numbers Matching, You Win $1,000 ");
            } else if ((guess1 == arr[0] || guess1 == arr[1] || guess1 == arr[2])
                    && (guess2 != arr[0] || guess2 != arr[1] || guess2 != arr[2])
                    && (guess3 != arr[0] || guess3 != arr[1] || guess3 != arr[2])

                    ||

                    (guess2 == arr[0] || guess2 == arr[1] || guess2 == arr[2])
                    && (guess1 != arr[0] || guess1 != arr[1] || guess1 != arr[2])
                    && (guess3 != arr[0] || guess3 != arr[1] || guess3 != arr[2])

                    ||

                    (guess3 == arr[0] || guess3 == arr[1] || guess3 == arr[2])
                    && (guess1 != arr[0] || guess1 != arr[1] || guess1 != arr[2])
                    && (guess2 != arr[0] || guess2 != arr[1] || guess2 != arr[2])

            )// close if
            {
                System.out.println("Any One Number Matching, You Win $10 ");
            } else {
                System.out.println("You Lose Sorry ");
            }

            System.out.println("Would you like to play again? Y/N ");
            PlayAgain = input.next();
            PlayAgain = PlayAgain.toUpperCase();

        }// end of while loop
    } // end of main

} // end of class

sorry if code is hard to read im a beginner at this stuff and i did use the ctrl + shift + f to format the code before posting.

Upvotes: 1

Views: 11844

Answers (2)

iDurocher
iDurocher

Reputation: 915

It would be best to break some logic into separate methods to handle code reuse. However, to keep it inline with what you showing and to keep the example easy to read, here is what you could do where you are entering the numbers:

        boolean isValid = false;
        while (!isValid) {
            System.out.print("Enter your lottery pick (0-9): ");

            int guess1 = -1;
            try {
                guess1 = input.nextInt();
            } catch (Exception e) {
                // Read any pending input which would be invalid characters at this point
                input.next();
            }

            if (guess1 > 9 || guess1 < 0) {
                System.out.println("Wrong input enter only 1 number between 0-9: ");
            } else {
                isValid = true;
            }
        }

Upvotes: 4

Bohemian
Bohemian

Reputation: 425013

Your code is "inelegant". Try using a pattern like this:

Pseudo java code:

while (true) {
    // get input
    if (input is "exit")
        break;
    if (input is bad)
        continue;
    // process input
}

Upvotes: 0

Related Questions