简体   繁体   中英

Try/Catch variable can't be initialized

I'm having an issue with my try/catch blocks. Now, my code works somewhat, however, when I enter in an invalid number ("a") for example, I have the program print out, ("please enter a valid number: "); But, I have a method that gets the object at the entered index and it's stating the local variable may not have been initialized.

do {
        int firstNum;
        int secondNum;
        int thirdNum;
        System.out
                .println("Please enter three of the numbers you see on the left of the shape:");
        try {

            firstNum = scan.nextInt();
            secondNum = scan.nextInt();
            thirdNum = scan.nextInt();

            Card card = Deck.randomizedCards.get(firstNum);
            Card card1 = Deck.randomizedCards.get(secondNum);
            Card card2 = Deck.randomizedCards.get(thirdNum);
            if (Deck.isSet(card, card1, card2) == true) {
                JOptionPane.showMessageDialog(panel, "You found a set!");
                Deck.completedSets.add(card);
                Deck.completedSets.add(card1);
                Deck.completedSets.add(card2);

            } else {
                JOptionPane.showMessageDialog(panel,
                        "You didn't find a set");
            }
             break;
        } catch (InputMismatchException name) {
            System.out
                    .println("You have entered an invalid number, please enter a number:");

        } catch (ArrayIndexOutOfBoundsException name) {
            System.out
                    .println("You have entered an invalid number, please enter a number:");

        }

    } while (true);

Since the assignment in the try statement might not occur you have your variables firstNum , secondNum , thirdNum left potentially unititialized.

You can circumvent that by either assigning a default value or by moving logic into the same try statement.

The compiler is (correctly) warning you that variables such as thirdNum might not have been initialized when, for example, the line secondNum = scan.nextInt(); throws an exception.

To circumvent this problem you should assign default values to all of your variables, or make the method return when an exception is thrown.

int firstNum;
int secondNum;
int thirdNum;
do {
    System.out.println("Please enter three of the numbers you see on the left of the shape:");
    try {
       firstNum = scan.nextInt();
       secondNum = scan.nextInt();
       thirdNum = scan.nextInt();
       Card card = Deck.randomizedCards.get(firstNum);
       Card card1 = Deck.randomizedCards.get(secondNum);
       Card card2 = Deck.randomizedCards.get(thirdNum);
       if (Deck.isSet(card, card1, card2)) {
           JOptionPane.showMessageDialog(panel, "You found a set!");
           Deck.completedSets.add(card);
           Deck.completedSets.add(card1);
           Deck.completedSets.add(card2);
           break;  
        } else {
           JOptionPane.showMessageDialog(panel, "You didn't find a set");
        }
    } catch (InputMismatchException name) {
        System.out
                .println("You have entered an invalid number, please enter a number (Range 1-11): ");

    } catch (ArrayIndexOutOfBoundsException name) {
        System.out
                .println("You have entered an invalid number, please enter a number(Range 1-11): ");
    }
} while (true);

The main problem lies in this code fragment

try{
   firstNum= scan.nextInt();
.
.
.
catch (InputMismatchException name) {
    System.out.println("You have entered an invalid number, please enter a number (Range 1-11): ");

}

when the input exception is thrown the offending input was left on the stream so when the execution reached scan.nextInt() again it got the same bad input and therefore threw the same exception and so on. Change the catch block to be:

 catch (InputMismatchException name) {
  String malformedGarbage = scan.Next();//processes the bad input so the scanner can move on.  
  System.out.println("You have entered an invalid number, please enter a number (Range 1-11): ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM