-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookGroupsCalculator.java
44 lines (42 loc) · 1.53 KB
/
BookGroupsCalculator.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
42
43
44
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;
import java.util.Hashtable;
public class BookGroupsCalculator {
private ArrayList<ArrayList<Integer>> dataMatrix;
private Hashtable<Integer,String> names;
public BookGroupsCalculator(String fileName) throws FileNotFoundException {
names = new Hashtable<Integer,String>();
File inpfile = new File(fileName);
Scanner in = new Scanner(inpfile);
in.useDelimiter("\n");
dataMatrix = new ArrayList<ArrayList<Integer>>();
int c = 0;
while(in.hasNext()) {
dataMatrix.add(new ArrayList<Integer>());
Scanner in2 = new Scanner(in.next());
in2.useDelimiter(" ");
names.put(c++,in2.next());
while(in2.hasNext()) {
dataMatrix.get(dataMatrix.size()-1).add(in2.nextInt());
}
}
}
public static void main(String args[]) throws FileNotFoundException{
BookGroupsCalculator calc = new BookGroupsCalculator("data.txt");
System.out.println(calc);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < dataMatrix.size(); i++) {
sb.append(names.get(i)+":\t");
for(int ii = 0; ii < dataMatrix.get(i).size(); ii++) {
sb.append(dataMatrix.get(i).get(ii)+" ");
}
sb.append("\n");
}
return sb.toString();
}
}