-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum.java
More file actions
26 lines (24 loc) · 835 Bytes
/
Sum.java
File metadata and controls
26 lines (24 loc) · 835 Bytes
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
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Sum {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("Ints.txt");
sumInts(file);
}
// This method sum all the MyInts in the collection
public static void sumInts(File file) throws FileNotFoundException {
Scanner readFromFile = new Scanner(file);
MyIntsCollection collection = new MyIntsCollection();
while (readFromFile.hasNextLine()) {
String line = readFromFile.nextLine();
Scanner readFromLine = new Scanner(line);
while (readFromLine.hasNext()) {
MyInt num = new MyInt(readFromLine.next());
collection.append(num);
}
System.out.println(collection.sumAll());
collection = new MyIntsCollection();
}
}
}