Создавая двойной массив (одна строка для состояний и одна для капиталов), я пытаюсь использовать «map.put» в цикле for для сохранения массивов «key (states)» и «value (capitols)» в HashMap. При использовании ключа из пользовательского ввода после назначения нового HashMap (hMap = getInfo () ;, мой вывод возвращает «null». Я не совсем уверен, что он делает неправильно, но у меня есть ощущение, что я сделал ошибку в for петля.
public class HashMapProgram {
public static void main (String[]args) {
Scanner input = new Scanner(System.in);
//Assign contents of map in getInfo to hMap
HashMap<String, String> hMap = getInfo();
//Prompting user to input a state (key)
System.out.print("Enter a state, or \"done\" when finished: ");
String state = input.next();
if(hMap.get(state) != "done")
System.out.println("The capital is "+ hMap.get(state));
}
public static HashMap<String, String> getInfo(){
//HashMap to save contents in
HashMap<String, String> map = new HashMap<>();
String x[][] = {
{"Alabama","Alaska","Arizona" ,"Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia",
"Hawaii" ,"Idaho" ,"Illinois" ,"Indiana" ,"Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington","West Virginia","Wisconsin","Wyoming"},
{"Montgomery","Juneau","Phoenix","Little Rock","Sacramento","Denver","Hartford","Dover","Tallahassee","Atlanta",
"Honolulu","Boise","Springfield","Indianapolis","Des Moines","Topeka","Frankfort","Baton Rouge","Augusta", "Annapolis",
"Boston","Lansing","St. Paul","Jackson","Jefferson City","Helena","Lincoln","Carson City","Concord","Trenton",
"Santa Fe","Albany","Raleigh","Bismarck","Columbus","Oklahoma City","Salem","Harrisburg","Providence","Columbia",
"Pierre","Nashville","Austin","Salt Lake City","Montpelier","Richmond","Olympia","Charleston","Madison","Cheyenne"}
};
//Saving contents in 'map'
for(int i = 0; i < x.length; i++) {
map.put(x[0][i], x[1][i]);
}
return map;
}
}