-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageConsoleInput.java
More file actions
84 lines (63 loc) · 1.84 KB
/
Copy pathAverageConsoleInput.java
File metadata and controls
84 lines (63 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.optum.samar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class AverageConsoleInput {
public static void main(String[] args) {
// System.out.println("Enter 5 numbers to calculate average. Separate each number with a space.");
//
// int[] input = readInput();
//
// System.out.print("You entered: ");
// for (int i = 0; i < input.length; i++) {
// System.out.print(input[i] + " ");
// }
//
// System.out.println();
//
// double avg = Utils.getAvg(input);
//
// System.out.println("Your average is " + avg);
System.out.println("Enter numbers to calculate average. Separate each number with a space.");
List<Integer> input = readInputList();
System.out.print("You entered: ");
for (Integer i : input) {
System.out.print(i + " ");
}
System.out.println();
double avg = Utils.getAvg(input);
System.out.println("Your average is " + avg);
}
private static int[] readInput() {
String input = "";
String[] list;
int[] returnList = new int[5];
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
input = br.readLine();
list = input.split(" ");
for (int i = 0; i < 5; i++) {
returnList[i] = Integer.parseInt(list[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
return returnList;
}
private static List<Integer> readInputList() {
String input = "";
String[] list;
List<Integer> returnList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
input = br.readLine();
list = input.split(" ");
for (String s : list) {
returnList.add(Integer.parseInt(s));
}
} catch (IOException e) {
e.printStackTrace();
}
return returnList;
}
}