0

Fairly new to coding and I want to only accept integers and display a JOptionPane warning message that integers are only allowed. I have tried to use a NumberFormatException but I am not familiar with this method. Thanks!

    import javax.swing.JOptionPane;
import java.util.Random;
public class GuessingGame
{
    public static void main(String[] args)
    {
        int start = JOptionPane.showConfirmDialog(null, "You must guess a number between 100 and 500 in a set amount of guesses.", "Press 'Yes' to start the guessing game.", JOptionPane.YES_NO_OPTION); 
        if(start == JOptionPane.YES_OPTION)
        {
            Random rng = new Random();
            int theNumber = rng.nextInt((500-100)+1)+100; 
            String guess;
            int maxAttempt = 1;
            int inputNumber;
            System.out.println(theNumber);
            while(maxAttempt<=4)
            {
                guess = JOptionPane.showInputDialog(null, "Please input a number between 100-500 you have four chances.");
                inputNumber = Integer.parseInt(guess);
                try
            {
            }catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, "You must only enter an integer", "Guessing Game", JOptionPane.WARNING_MESSAGE);
            }
                if(inputNumber==theNumber)
                {
                    JOptionPane.showMessageDialog(null, "Winner! Guess is correct.", "Guessing Game", JOptionPane.INFORMATION_MESSAGE);
                    break;
                }
                else if(inputNumber>theNumber)
                    JOptionPane.showMessageDialog(null, "Guess was greater than the real number.", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                else
                    JOptionPane.showMessageDialog(null, "Guess was less than the real number.", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                maxAttempt++;
            }

            if(maxAttempt>4)
                JOptionPane.showMessageDialog(null, "Game Over. You have exceeded your guessing limit.", "Guessing Game", JOptionPane.ERROR_MESSAGE);
        }
        else
            JOptionPane.showMessageDialog(null, "Quiting Game, press 'Ok' to exit.", "Guessing Game", JOptionPane.INFORMATION_MESSAGE);




    }
}
Magix
  • 5
  • 1
  • 4
  • You put `try` in the `while` loop, where it should be right at the start of the `main()` method before you accept the first `int` as input. – Max Voisard Mar 08 '20 at 20:16
  • Does this answer your question? [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) – B. Go Mar 08 '20 at 22:38

1 Answers1

0

What you need to do is call Integer#parseInt inside of the try-catch. Then When an exception occurs you need to continue to the next iteration of the while-loop.

Replace

                inputNumber = Integer.parseInt(guess);
                try  {
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null, "You must only enter an integer", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                }

With

                try {
                    inputNumber = Integer.parseInt(guess);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "You must only enter an integer", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                    continue;
                }
Jason
  • 5,154
  • 2
  • 12
  • 22