简体   繁体   中英

Run program again from the start if error was found (try-catch inside while loop) Java

Program description: User enter elements for the array, and then it returns the 2 middle elements of the array


I'm trying to implement implement try-catch statement inside the while loop. The idea is, when it catches an error, program starts running again (From the point of entering the number of elements in the array). The problem is that the while loop in my code, works fine for the else statement:

} else {
                System.out.println("ERROR: Incompatible Array Size ");
                runAgain = true;
            }
            }`

But for the catch expressions, I end up in an endless loop


Code:

public class MiddleArray {

public static int[] makeMiddle(int[] nums) {

    int[] newList = new int[2];

    if (nums.length>2) {

        int middle = nums.length/2;
        newList[0] = nums[middle-1];
        newList[1] = nums[middle];
        return newList;
    }

    else {
        return nums;
    }

}

public static void main(String[] args) {
    int  numOfElements; //number of elements in the array
    int userInput;
    boolean runAgain;


    Scanner input = new Scanner(System.in);

    System.out.print("Please enter the number of elements that you wanna have in array: ");



    try {
        numOfElements = input.nextInt();

        if (numOfElements % 2 == 0 && numOfElements != 0) {
            int[] arrayOfNumbers = new int[numOfElements];


            for (int i = 0; i <= arrayOfNumbers.length - 1; i++) {
                System.out.print("Element " + (i+1) + ": ");
                userInput = input.nextInt();
                arrayOfNumbers[i] = userInput;
            }
            int[] middleOfArray = makeMiddle(arrayOfNumbers); //return

            System.out.print("For the array " +
                    Arrays.toString(arrayOfNumbers) + " the 2 middle elements is " + Arrays.toString(middleOfArray));
        } else {
            System.out.print("ERROR: Incompatible Array Size ");
        }

    }

    catch(InputMismatchException e) {
        System.out.println("ERROR: Incompatible Data Type");
    }
    catch (NegativeArraySizeException e) {
        System.out.println("ERROR: Incompatible Array Size");
    }
}

}

Add do.. while instruction:

do {
   try {
        numOfElements = input.nextInt();

        if (numOfElements % 2 == 0 && numOfElements != 0) {
            int[] arrayOfNumbers = new int[numOfElements];


            for (int i = 0; i <= arrayOfNumbers.length - 1; i++) {
                System.out.print("Element " + (i+1) + ": ");
                userInput = input.nextInt();
                arrayOfNumbers[i] = userInput;
            }
            int[] middleOfArray = makeMiddle(arrayOfNumbers); //return

            System.out.print("For the array " +
                    Arrays.toString(arrayOfNumbers) + " the 2 middle elements is " + Arrays.toString(middleOfArray));
            return; // !!!THIS STOP WHILE!!!
        } else {
            System.out.print("ERROR: Incompatible Array Size ");
        }

    }

    catch(InputMismatchException e) {
        System.out.println("ERROR: Incompatible Data Type");
    }
    catch (NegativeArraySizeException e) {
        System.out.println("ERROR: Incompatible Array Size");
    }
} while(true)

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