Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble correctly handling exceptions

Tags:

java

I'm a beginner Java programmer writing a program that calculates the average of a set of positive integers. First, it queries the user for the amount of integers to be entered. Then, it collects the integers from the user and outputs the calculated average. I'm having trouble handling the exceptions. When the user tries to enter a negative number to be averaged, the exception is correctly displayed, but it doesn't correctly continue the for-loop to collect the appropriate amount of numbers. For example, below is an example output:

Please enter the number of integers to be averaged: 5
Enter a number: 1
Enter a number: 2
Enter a number: -3
NegativeIntegerException: N must be a positive integer.
Enter a number: 3
Enter a number: 4
The average is: 0.0

It correctly threw the exception, but didn't continue to fill the 5 integers. Only the other 4 that were valid.

Below is the code:

import java.util.*;

class NegativeIntegerException extends Exception {

    public NegativeIntegerException()
    {
        super("N must be a positive integer.");
    }
}


public class intAverage {

    public static void main(String[] args)
    {
        int N = 0; //number of integers to be averaged
        int[] numbers = null; //array to hold integers
        int sum = 0;
        int newInt;
        double average;
        Scanner keyboard = new Scanner(System.in);
        int x = 1; //for do-while loop #1
        int z = 1; //for do-while loop #2

        do {
        try {
            System.out.print("Please enter the number of integers to be averaged: ");
            N = keyboard.nextInt();
            numbers = new int[N]; //setting size of array
            x = 2;
        } 
        catch (Exception e) {
            System.out.println("N must be a positive integer");
        }
        } while (x == 1);

        do {
            for(int i = 0; i < N; i++) //collecting the integers
            {
                System.out.print("Enter a number: ");
                newInt = keyboard.nextInt();
                if (newInt < 0) {
                    try {
                        throw new NegativeIntegerException();
                    }
                    catch(NegativeIntegerException e) {
                        System.out.println(e);
                    }
                }
                newInt = numbers[i];
            }
            z = 2;
        } while (z == 1);


        for(int y = 0; y < N; y++) //calculate average
        {
            sum = sum + numbers[y];
        }
        average = sum / N;

        System.out.println("The average is: " + average);

    }

}
like image 399
Daksakar Avatar asked Apr 24 '26 10:04

Daksakar


1 Answers

When you print out the exception, subtract 1 from i, and continue. This will basically restart the iteration.

do {
    for(int i = 0; i < N; i++) //collecting the integers
    {
        System.out.print("Enter a number: ");
        newInt = keyboard.nextInt();
        if (newInt < 0) {
            try {
                throw new NegativeIntegerException();
            }
            catch(NegativeIntegerException e) {
                System.out.println(e);
                // Ignore this input
                i--;
                continue;
            }
        }
        numbers[i] = newInt;
    }
    z = 2;
} while (z == 1);

Note that you don't need to throw an exception for this to work - you could just as easily say:

do {
    for(int i = 0; i < N; i++) //collecting the integers
    {
        System.out.print("Enter a number: ");
        newInt = keyboard.nextInt();
        if (newInt < 0) {
            System.out.println("NegativeIntegerException: N must be a positive integer");
            // Ignore this input
            i--;
            continue;
        }
        numbers[i] = newInt;
    }
    z = 2;
} while (z == 1);

This is more readable, too.

like image 73
Andrew Williamson Avatar answered Apr 26 '26 00:04

Andrew Williamson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!