mercredi 25 décembre 2019

How do I prevent or redo a SWING input when I get a NumberFormatException?

I'm making a lottery program that takes 3 separate integer inputs from the user and compares it to 3 random ONE-DIGIT inputs (I used 2 arrays, one for random, and the other for the user inputs). I've managed to make the check if the input is more than one digit long, but I can't figure out how to make one when my program receives an string or char input.

Line 31-42 is the block of code where it checks the user input

import javax.swing.*;

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

        final int HIGHEST_VALUE = 10;
        final int LOWEST_VALUE = 0;
        final int LIMIT = 3;

        int[] array1 = new int[LIMIT];
        int[] array2 = new int[LIMIT];
        int[] array3 = new int[LIMIT];

        int randomValue;
        int numberInput;
        int numberMatched = 0;

        boolean exactOrder = true;

         //taking 3 random numbers and storing into array1//
        for(int counter = 0; counter < LIMIT; counter++) {
            randomValue = ((int)(Math.random() * 100) % HIGHEST_VALUE + LOWEST_VALUE);
            array1[counter] = randomValue;
        }

        for(int counter = 0; counter < LIMIT; counter++) {
            array3[counter] = array1[counter];
        }

        //Taking 3 numbers from user and storing into array2//
        JOptionPane.showMessageDialog(null,"Enter 3 one-digit positive numbers for your 3 guesses");
         for(int counter = 0; counter < LIMIT; counter++) {
             String num1= JOptionPane.showInputDialog("Please input Guess #" + (counter+1));
             numberInput = Integer.parseInt(num1);
             if(numberInput < 0 || numberInput > 9) {
                 JOptionPane.showMessageDialog(null,"Please input a ONE-DIGIT POSITIVE Number for:");
                 JOptionPane.showMessageDialog(null,"Guess #" + (counter+1));
                 num1= JOptionPane.showInputDialog("Please input Guess #" + counter);
                 numberInput = Integer.parseInt(num1);
             }
             array2[counter] = numberInput;
         }

         //Checks for how many numbers matched//
         for (int counter = 0; counter < LIMIT; counter++) {
             for (int counter2 = 0; counter2 < LIMIT; counter2++) {

                 if(array2[counter] == array3[counter2]) {
                     numberMatched++;
                     array3[counter2] = 99;
                     break;
                 }
             }
         }
             //If 3 numbers matched, it checks if it is in order//
        if(numberMatched == 3) {
            for (int counter = 0; counter < LIMIT; counter++) {
                if(array2[counter] == array1[counter]) {

                }
                else {
                    exactOrder = false;
                    continue;
                }
            }
        }

        //Result Checking//
        if(numberMatched == 3 && exactOrder == true) {
            JOptionPane.showMessageDialog(null,"You won 1000000. All numbers match, and are in order.");
        }
        else {
            if(numberMatched == 3) {
                JOptionPane.showMessageDialog(null,"You won 1000. All Numbers match.");
            }
            else {
                if(numberMatched == 2) {
                    JOptionPane.showMessageDialog(null,"You won 100. 2 Numbers match.");
                }
                else {
                    if(numberMatched == 1) {
                        JOptionPane.showMessageDialog(null,"You won 10. 1 Number matched.");
                    }
                    else {
                        JOptionPane.showMessageDialog(null,"No Matches");
                    }
                }
            }       
        }
        //Result Display//
        JOptionPane.showMessageDialog(null,"The winning numbers were:" + "\n" + array1[0] + " " + array1[1] + " " + array1[2] + "\n You entered:" + "\n" + array2[0] + " " + array2[1] + " " + array2[2] );
    }
}

Aucun commentaire:

Enregistrer un commentaire