Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Exercise5 {
- public static void main(String args[]) {
- String input;
- int total = 0;
- double avg = 0;
- ArrayList<Integer> numList = new ArrayList<Integer>();
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter integer to add to total, else find total average:");
- while (sc.hasNextInt()) {
- System.out.println("Enter integer to add to total, else find total average:");
- input = sc.nextLine();
- // add input into ArrayList
- try {
- numList.add(Integer.parseInt(input));
- } catch(NumberFormatException e) {
- System.out.println("Hello");
- break;
- }
- // your code goes above
- }
- // iterate through ArrayList, add elements to total, then calculate average
- for (int i = 0; i < numList.size(); i++) {
- avg += numList.get(i);
- }
- if (numList.size() != 0) {
- avg /= numList.size();
- }
- // your code goes above
- System.out.println(avg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment