Answered step by step
Verified Expert Solution
Link Copied!

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:

image text in transcribed 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 ArrayList array = 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

LO2 Explain the nature of the psychological contract.

Answered: 1 week ago

Question

LO1 Discuss four different views of motivation at work.

Answered: 1 week ago