This what i have public void loadDistance(File fileDistance) throws FileNotFound
ID: 3538882 • Letter: T
Question
This what i have
public void loadDistance(File fileDistance) throws FileNotFoundException
{
Scanner distanceScan= new Scanner(fileDistance);
}
How Can I do this??
loadDistance method: reads from cityDistance.txt and fills distance 2-dimensions array. This method defines
Scanner variable(s) to read input file(s), initialize the distances array to -1(all elements to -1, then dimension-index to 0)
read the input file(s), calls findIndexCity method to do the city-validation (both cities MUST exist in cities array),
display 2 cities and the distance between and fill the distances array. E.g.
distances [0][0] =0; [0][1] =51; [0][2] =-1;
distances [1][0] =51; [1][1] =0; [1][2] =66;
distances [2][0] =-1; [2][1] =66; [2][2] =0;
([0][0] means from kalamazoo to kalamazoo =0; [1][1] from grandRapids to grandRapids; [2][2] from lansing to lansing)
(Because this is the same [0][1] means from kalamazoo to grandrapids =51 and [1][0] from grandRapids to kalamazoo= 51; you have to register
both ways automatically)
Explanation / Answer
public void loadDistance(File fileDistan)
{
for(int i=0; i<distances.length; i++)
for(int j=0; j<distances[i].length; j++)
{
if(i==j) distances[i][j]=0;
else distances[i][j] = -1;
}
try
{
Scanner distanceScan= new Scanner(fileDistance);
while (distanceScan.hasNextLine())
{
String line = distanceScan.nextLine();
String[] city = line.split(",");
distances[findIndexCity(city[0])][findIndexCity(city[1])] = Integer.parseInt(city[2]);
distances[findIndexCity(city[1])][findIndexCity(city[0])] = Integer.parseInt(city[2]);
}
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage()); }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.