▼何をしたいのか
変数 String key にシステムから入力値が与えられている。
下記の情報を持つHashMapインスタンスを作成し、入力値である変数keyを用いて値を取得し、出力したい。
key : value
"name" : "技育助武"
"age" : "20"
"address" : "東京"
▼何が起こっているのか
エラー文
Main.java:10: error: variable key is already defined in method main(String[])
HashMap<String, String> key = new HashMap<String, String>();
^
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:16: error: cannot find symbol
ArrayList<HashMap> data = new ArrayList<HashMap>();
^
symbol: class ArrayList
location: class Main
Main.java:16: error: cannot find symbol
ArrayList<HashMap> data = new ArrayList<HashMap>();
^
symbol: class ArrayList
location: class Main
6 errors
Main.java:10: error: variable key is already defined in method main(String[])
HashMap<String, String> key = new HashMap<String, String>();
^
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:16: error: cannot find symbol
ArrayList<HashMap> data = new ArrayList<HashMap>();
^
symbol: class ArrayList
location: class Main
Main.java:16: error: cannot find symbol
ArrayList<HashMap> data = new ArrayList<HashMap>();
^
symbol: class ArrayList
location: class Main
6 errors
Main.java:10: error: variable key is already defined in method main(String[])
HashMap<String, String> key = new HashMap<String, String>();
^
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:13: error: cannot find symbol
key.put("name","技育助武");key.put("age","20");key.put("address","東京");
^
symbol: method put(String,String)
location: variable key of type String
Main.java:16: error: cannot find symbol
ArrayList<HashMap> data = new ArrayList<HashMap>();
^
symbol: class ArrayList
location: class Main
Main.java:16: error: cannot find symbol
ArrayList<HashMap> data = new ArrayList<HashMap>();
^
symbol: class ArrayList
location: class Main
6 errors
▼ソースコード ※スレッドに添付
import java.util.Scanner; import java.util.HashMap; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String key = scan.next(); //HashMapインスタンス作成 HashMap<String, String> key = new HashMap<String, String>(); //HashMapに情報を格納 key.put("name","技育助武");key.put("age","20");key.put("address","東京"); //key情報を入れる配列 ArrayList<HashMap> data = new ArrayList<HashMap>(); //keyの情報をArrayListに追加 data.add(key); //key情報を表示 if(key.equals("name")){ System.out.println(data.get(0)); }else if(key.equals("age")){ System.out.println(data.get(1)); }else if(key.equals("address")){ System.out.println(data.get(2)); } } }