Java: distance between two cities using array

Acme Trucking Company has hired you to write software to help dispatch its trucks. One important element of this software is knowing the distance between any two cities that it services. Design and implement a Distance class that stores the distances between cities in a two-dimensional array. This class will need some way to map a city name, Boise, into an integer that can be used as an array subscript. The class should also contain methods that would make it useful for looking up the distance between two cities. Another useful method would tell the user the closest city to a given city

Take a look at the code below and really try to understand how it’s working. The BufferedReader with InputStreamReader is a little less convenient than Scanner for beginners; but it’s nice to know how it works since it’s much faster and essential to use for many of CodeChef’s problems in Java.

Sample input:

Boise, Phoenix, Detroit, Chicago, Los Angeles

6

0 2 8

1 2 31

1 3 17

3 1 55

4 3 290

4 4 9

print cities

print paths

Sample output:
There are 5 cities.

cities[0]= Boise

cities[1]= Phoenix

cities[2]= Detroit

cities[3]= Chicago

cities[4]= Los Angeles

The distance from Boise to Detroit is 8

The distance from Phoenix to Detroit is 31

The distance from Phoenix to Chicago is 17

The distance from Chicago to Phoenix is 55

The distance from Los Angeles to Chicago is 290

public class Distance {

private static String[] cities = new String[0];
private static int[][] paths = new int[0][0];

public static void main(String[] args)throws java.io.IOException{
	
	java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
	
	cities = reader.readLine().split(", ");
	paths = new int[cities.length][cities.length];
	
	int numberOfPaths = Integer.parseInt(reader.readLine());
	
	for(int i = 0; i < numberOfPaths; i += 1){
		String[] input = (reader.readLine().split(" "));
		createPath(Integer.parseInt(input[0]), Integer.parseInt(input[1]), Integer.parseInt(input[2]));
	}
	
	while(true){
		String input = reader.readLine();
		if(input.equals("end")) //terminate program
			break;
		else if(input.equals("print cities"))
			printCities();
		else if(input.equals("print paths"))
			printPaths();
		else if(input.equals("distance")){ //1. type distance & hit enter.  2. type two cities in terms of their ID, separated by a space
			String[] twoCities = reader.readLine().split(" ");
			distance(Integer.parseInt(twoCities[0]), Integer.parseInt(twoCities[1]));
		}
		/* Directions for finding distance between two cities:
		 * 1. Type  distance  & hit enter.
		 * 2. Type ID of first city and ID of second city separated by a space.
		 * 
		 * example:
		 * distance
		 * 0 2
		 * Output: The distance from Boise to Detroit is 8
		 */
		
		//try to figure out a method to find the closest city

// else if(input.equals(“closest city”)){
// int city = Integer.parseInt(reader.readLine());
// closestCity(city);
// }
}
}//end main()

/** Print list of cities.
 */
public static void printCities(){
	System.out.println("There are " + cities.length + " cities.");
	for(int i = 0; i < cities.length; i += 1)
		System.out.println("cities[" + i + "]= " + cities[i]);
}

/** Print list of current routes between cities.
 */
public static void printPaths(){
	for(int y = 0; y < paths.length; y += 1){
		for(int x = 0; x < paths[y].length; x += 1)
			if(paths[y][x] != 0)
				System.out.println("The distance from " + getCity(y) + " to " + getCity(x) + " is " + paths[y][x]);
	}
}

/** Return the string name of a city's ID #.
 */
public static String getCity(int n){
	return cities[n];
}

/** Create a path from city 'from' to city 'to' that is 'distance' long.
 */
public static void createPath(int from, int to, int distance){
	if(from == to) //the distance from a city to itself is 0...
		return;
	paths[from][to] = distance;
}

/** Print the distance from city 'from' to city 'to'.
 */
public static void distance(int from, int to){
	if(paths[from][to] != 0)
		System.out.println("The distance from " + getCity(from) + " to " + getCity(to) + " is " + paths[from][to]);
	else
		System.out.println("There is no direct from from " + getCity(from) + " to " + getCity(to) + ".");
}

}//end class

What is your question? Do you want us to write your homework for you?