'Java - Passing 2D array to function
-------------------------------------------------------------
| | herndon | fairfax | baltimore | centerville |
-------------------------------------------------------------
| herndon | 0 | 50 | 100 | 20 |
-------------------------------------------------------------
| fairfax | 50 | 0 | 70 | 110 |
-------------------------------------------------------------
| baltimore | 100 | 70 | 0 | 200 |
-------------------------------------------------------------
| centerville | 20 | 110 | 200 | 0 |
-------------------------------------------------------------
Java - Passing 2D array to function
1) defining a 2D array for distance and populate it -> done ( Its shown in program )
2) defining a city array -> done ( Its shown in program )
3) ask the user for soure and destination of the city :
e.g.
Enter from city:
Enter To city:
Lets use JOptionPane and Error handling function when user enters cityNames.
4)call function" findDistance "and passsing array of citynames ,array of distances to this function.
5) call function " display" distance <- showing the distances of those cities that was entered by user .
problems i am having :
a) passing arrays of citynames and array of distance to findDistance function
(followup #4)
b) passing the argumants to function display. (followup #5)
here is what i did so Far !
package test;
class Test1 {
public static void main(String[] args) {
int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}}; // step 1
String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"}; // step 2
findDistance(distances); // -> a . 1st problem , i am having problem to pass citynames and distances to this function.
// display () // -> b . 2nd problem i am having : i have a issue to pass the arguments to this function to Display Distances between 2 cities .
public static void findDistance(String[] names, int[] dist, int[] src) {
for(int i = 0; i < src.length; i++) {
// i would like to use JOptioanPane function and error handling
// instead of system.out.println.
System.out.println("enter from city" + src[i]);
}
for(int i = 0; i < dist.length; i++) {
System.out.println("enter from city" + dist[i]);
}
int i = 0;
dist[i] = src[i];
return;
}
}
Solution 1:[1]
Based on your question and the given code, I wrote the below class. I intentionally left method bodies empty so you'll need to figure out some details there yourself. If you can fill bodies of findDistance and display methods you can accomplish what you want.
package Test;
import java.util.Scanner;
class Test1 {
public static void main(String[] args) {
int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}};
String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};
Scanner keyboard = new Scanner(System.in);
System.out.println("enter from city:");
String src = keyboard.nextLine();
System.out.println("enter to city:");
String dest = keyboard.nextLine();
int distance = findDistance(CityNames, distances, src, dest);
display(distance);
}
public static int findDistance(String[] CityNames, int[][] distances, String src, String dest) {
// Find and return the distance between src and dest.
return 0;
}
public static void display(int distance) {
// Display the distance to user.
}
}
Solution 2:[2]
package test;
import javax.swing.JOptionPane;
class ArrayTest {
public static void main(String[] args) {
int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}};
String cityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};
int departureIndex = findCityIndex("departure", cityNames);
int destinationIndex = findCityIndex("destination", cityNames);
display(departureIndex, destinationIndex, distances);
}
private static int findCityIndex(String cityType, String[] cityNames) {
boolean cityNotFound = true;
int index = 0;
while(cityNotFound)
{
String cityName= JOptionPane.showInputDialog("Please enter " + cityType + " city");
for (int i = 0; i < cityNames.length; i++)
{
if (cityName.equalsIgnoreCase(cityNames[i]))
{
index = i;
cityNotFound = false;
}
}
if (cityNotFound)
JOptionPane.showMessageDialog(null, "Invalid city name. Please reenter.");
}
return index;
}
private static void display(int departureIndex, int destinationIndex, int[][] distances) {
JOptionPane.showMessageDialog(null, "Distance is " + distances[departureIndex][destinationIndex]);
}
}`enter code here`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Root G |
| Solution 2 | sammy |
