-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem17.java
More file actions
59 lines (56 loc) · 1.38 KB
/
Problem17.java
File metadata and controls
59 lines (56 loc) · 1.38 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
class MapManager{
private static Map<String,Double> map = new TreeMap<String,Double>(){
public String toString() {
String print = new String();
Set<Map.Entry<String,Double>> set = map.entrySet();
Iterator<Map.Entry<String,Double>> it = set.iterator();
while(it.hasNext()) {
Map.Entry<String,Double> e = it.next();
print += e.getKey()+ " " + e.getValue() + "\n";
}
return print.trim();
}
};
public static Map<String, Double> readData(String f){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
}catch (FileNotFoundException e) {
return null;
}
while(true) {
try {
String line = br.readLine();
if(line == null) break;
String[] a = line.split(" ");
map.put(a[0], Double.parseDouble(a[1]));
} catch(IOException e) {
e.printStackTrace();
}
}
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
return map;
}
}
public class Problem17 {
public static void main(String[] args) {
Map<String, Double> map = MapManager.readData("input.txt");
if(map == null) {
System.out.println("Input file not found.");
return;
}
System.out.println(map);
}
}