java - HashMap - getting all the values associated with a key -


i created hashmap each key contains arraylist value. i'm having trouble understanding how fetch arraylist associated key , values stored in it. see below error getting when running program.

import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.scanner;  public class flights {  private hashmap<string, arraylist<string>> flights = new hashmap<string, arraylist<string>>();  private void readflights(string filename) {     try {         bufferedreader bf = new bufferedreader(new filereader(filename));          while (true) {             string line = bf.readline();             if (line == null) {                 break;             }             if (!line.isempty()) {                 string fromcity = line.substring(0, line.indexof("-"));                 string tocity = line.substring(line.indexof(">") + 2);                 arraylist<string> city = flights.get(fromcity);                 if (city != null) {                     city.add(tocity);                 } else {                     arraylist<string> destinations = new arraylist<string>();                     destinations.add(tocity);                     flights.put(fromcity, destinations);                 }             }          }         bf.close();     } catch (ioexception e) {         e.printstacktrace();     } }  private void printcities() {     iterator<string> fi = flights.keyset().iterator();     while (fi.hasnext()) {         string next = fi.next();         system.out.println(next + "-> "+ flights.get(next));     } }  @suppresswarnings("resource") private void printwelcome() {     system.out             .println("welcome flight planner.\nhere list of of our cities:");     printcities();     system.out.print("enter starting city.");     scanner in = new scanner(system.in);     string input = in.nextline();     system.out.println("from " + input + " can fly directly to:");     printavailableflights(input); }  private void printavailableflights(string city) {     arraylist<string> origin = flights.get(city);     (string cities: origin) {         system.out.println(cities);     } }  public static void main(string[] args) {     flights f = new flights();     f.readflights("flights.txt");     f.printwelcome(); }  } 

here flights.txt file:

san jose -> san francisco san jose -> anchorage  new york -> anchorage new york -> san jose new york -> san francisco new york -> honolulu  anchorage -> new york anchorage -> san jose  honolulu -> new york honolulu -> san francisco  denver -> san jose  san francisco -> new york san francisco -> honolulu san francisco -> denver 

and here see in console when running program:

welcome flight planner. here list of of our cities: honolulu -> [new york, san francisco] denver -> [san jose] anchorage -> [new york, san jose] san francisco -> [new york, honolulu, denver] new york -> [anchorage, san jose, san francisco, honolulu] san jose -> [san francisco, anchorage] enter starting city.denver denver can fly directly to: exception in thread "main" java.lang.nullpointerexception @ flights.printavailableflights(flights.java:64) @ flights.printwelcome(flights.java:59) @ flights.main(flights.java:72) 

this line

string fromcity = line.substring(0, line.indexof("-")); 

leaves space after starting city because how text file formatted. keys "denver " , "san jose ". should change this:

string fromcity = line.substring(0, line.indexof("-") - 1); 

additionally, make more sense use entire delimiter " -> " because cities have dash in them, such wilkes-barre.

nullpointerexception thrown because map returns null if key doesn't exist. should account in printavailableflights.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -