Skip to content

Commit 54a929e

Browse files
author
Moeko Takagi
committed
A
HashMap
1 parent 07e076c commit 54a929e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

q2/moeko/AssociativeArray.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
data.txtに書かれているデータを読み込んで
3+
member[名前]["weight"],member[名前]["height"]で身長、体重にアクセス出来る連想配列を作れ
4+
ただし、data.txtのデータは"名前,身長,体重"となっている
5+
*/
6+
7+
import java.util.HashMap;
8+
import java.io.FileReader;
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.FileNotFoundException;
12+
13+
public class AssociativeArray{
14+
public static void main(String[] args){
15+
HashMap<String, HashMap<String, Double>> map = new HashMap<String, HashMap<String, Double>>();
16+
try{
17+
BufferedReader reader = new BufferedReader(new FileReader("../data.txt"));
18+
String s = reader.readLine();
19+
while(s != null){
20+
String[] strs = s.split(",", 0);
21+
map.put(strs[0], new HashMap<String, Double>());
22+
map.get(strs[0]).put("height", Double.parseDouble(strs[1]));
23+
map.get(strs[0]).put("weight", Double.parseDouble(strs[2]));
24+
s = reader.readLine();
25+
}
26+
}catch(FileNotFoundException e){
27+
System.out.println(e);
28+
}catch(IOException e){
29+
System.out.println(e);
30+
}
31+
System.out.println(map.get("sato").get("height"));
32+
System.out.println(map.get("uchimura").get("weight"));
33+
}
34+
}
35+
36+

0 commit comments

Comments
 (0)