Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Keep hitting error with the arraylist, dont know how can I fix it. And how to I impliment catch to the program? Thank you Code:
Keep hitting error with the arraylist, dont know how can I fix it. And how to I impliment catch to the program? Thank you Code: package AverageProblem; import java.util.ArrayList; import java.util.Scanner; /** * Created by EdwardLai on 2/26/17. * * Write a program which calculates the average of N integers. The program should prompt the user to enter * the value of N. Afterwards the user must enter all N of the integers. If the user enters a non- positive * value for any of the integers, an exception should be thrown (and caught) with the message N must be positive. If there is any exception as the user is entering the N numbers, an error message should be displayed and the user prompted to enter the number again. Save your solution in Average.java. */ public class Average { private static int intergerNum; private static int loopcount=0; private static ArrayListarray = new ArrayList(); static Scanner scanner= new Scanner(System.in); public static void main (String[] args) { System.out.println("This is a interger average calculator "); while(intergerNum != 9999) { System.out.println("Please input a positive interger: "); intergerNum = scanner.nextInt(); if(intergerNum != 9999) { checkPositive(intergerNum); array.add(loopcount,intergerNum); loopcount++; } } double sum = 0.0; for(int i : array ) { sum+=array.get(i); } System.out.println("The average is: "+ (double)(intergerNum/loopcount)); } public static void checkPositive(int num) { if (num write a program which calculates the average of N integers. The program should prompt the user to enter the value of N. Afterwards the user must enter all N of the integers If the user enters a non- positive value for an of the integers, an exception should be thrown (and caught with the message "N must be positive." If there is any exception as the user is entering the N numbers, an error message should be displayed and the user prompted to enter the number again
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started