-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
41 lines (32 loc) · 1.19 KB
/
Main.java
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
import java.io.File;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
File myFile = new File("Assignment1.txt");
try {
Scanner myScanner = new Scanner(myFile);
while (myScanner.hasNextLine())
{
String line = myScanner.nextLine();
String[] tokens = line.split(",");
int length = tokens.length;
// Fetch the name
String name = tokens[0];
// Add up the sums
double sum = 0;
for (int i = 1; i < length; ++i)
// changing values in ++i gives error and not infinite loop.
{
// probably the right way to parse a double?
double value = Double.parseDouble(tokens[i]);
// double value = Double.valueOf(tokens[i]);
sum += value;
}
System.out.println("Name: " + name + ", Average Spent: " + (sum / (length - 1)));
}
myScanner.close();
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
}