Member Avatar for loserspearl

I have my program all working but whenever a user clicks cancel or exit I want the program to close, not throw my catch. Whenever a user throws a catch they have to input their answer twice before it will go back to normal operations.

import java.util.*;
import javax.swing.*;

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

        int playAgain = 0;
        boolean win = false; 
	
	//play the game once, tell user if they won or lost, and play again if they so choose
        do {
            if (playGame(win) == false)	
                playAgain = JOptionPane.showConfirmDialog(null, "You lost, play again?", "Choose one", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            else
                playAgain = JOptionPane.showConfirmDialog(null, "You are correct! Play again?", "Choose one", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        } while (playAgain == 0);
    }
	
    static boolean playGame (boolean win) {

        int chance = 4;			//chances total
        int guessCtr = 0;		//guesses made
        int guess = 0;
        String guessStr;
        String chanceStr = "" + chance + " chances left";
	String message = "Guess a number 1 to 10.";			 
	
        Random rand = new Random();
        int answer = rand.nextInt(9) + 1;  //gives random number 0 to 9 and adds 1 to make 1-10.
	
	//prompts the user for input	
        guessStr = JOptionPane.showInputDialog(null, message, chanceStr, JOptionPane.QUESTION_MESSAGE);

	//play a full game while users has chances left
	while ( chance > 1 ) {
            try {
                guess = Integer.parseInt(guessStr);
                //tells the use to enter a valid integer
		if (guess < 0 || guess > 10)
                    message = "Enter a number between 1 and 10.";
                    guessStr = JOptionPane.showInputDialog(null, message, chanceStr, JOptionPane.ERROR_MESSAGE);
            }
            catch (Exception ex) {
                //error handling for invalid inputs such as letters
		message = "Invalid input, please try again.";
                guessStr = JOptionPane.showInputDialog(null, message, chanceStr, JOptionPane.ERROR_MESSAGE);
                continue;
            }
                chance--;
                chanceStr = "" + chance + " chances left";
                // calls compareTo to know if answer is correct, too high, or too low		
                if ( compareTo( guess, answer ) == 0){	
                    //user wins
                    return true;    
		}
                else if ( compareTo( guess, answer ) < 0){
                    message  = "Your guess was too low.";
                    guessStr = JOptionPane.showInputDialog(null, message, chanceStr, JOptionPane.QUESTION_MESSAGE);
                   
                }	
                else {
                    message = "Your guess was too high.";
                    guessStr = JOptionPane.showInputDialog(null, message, chanceStr, JOptionPane.QUESTION_MESSAGE);
                }

        }
	//the user ran out of chances and lost
        return false;
    }

    static int compareTo ( int compGuess, int compAnswer ) {
	//checks if the users input is correct, higher, or lower
        if ( compGuess == compAnswer)
            return 0; 	
        else if ( compGuess < compAnswer)
            return -1;
	else
            return 1;   
    }
}
try {
guess = Integer.parseInt(guessStr);
//tells the use to enter a valid integer
if (guess < 0 || guess > 10)
message = "Enter a number between 1 and 10.";
guessStr = JOptionPane.showInputDialog(null, message, chanceStr, JOptionPane.ERROR_MESSAGE);
}
catch (Exception ex) {

Try catching an InputMismatchException instead of an Exception. The code in the catch block executes if the input is not an integer.

Member Avatar for loserspearl

I did that and the code would I tried that already and it exits my program to the command line with a NumberFormatException if i tried to use a character. I want it to ignore the invalid input and try again or exit if they click cancel or X.

catch (InputMismatchException ex) {

Catch all the exceptions that are thrown.NumberFormatException is thrown appropriately here as you are trying to parse a character to integer. Catch that as well !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.