0

Been fiddling around with my main method's looping for the last hour or so, to no avail. The issue is coming from Array Out of Bounding in both the values array and the identity matrix.

Assume all other methods etc. are functional.

public static void main(String args[])
{
    double adjacency_matrix[][] = new double[80][80];
    double[] values = new double[160];
    String[]contents = new String[160];

    FileIO reader = new FileIO();
    contents = reader.load("C:\\Users\\Mark\\Documents\\Java Workspace\\CS211\\src\\TSP\\locations.txt");

    double temp1,temp2,temp3,temp4;
    int count = 0,count2 = 0;

    for(int i=0; i<values.length; i++)
    {
        values[i] = Double.parseDouble(contents[i].substring(0,contents[i].length()-1));
    }

    for(int i=0; i<=79;i++)
    {
        for(int j=0; j<=79; j++)
        {
            if(i == j)
            {
                adjacency_matrix[i][j] = 0.0;

            count2+=1;
            }

            else
            {
                temp1 = values[i+count];
            temp2 = values[i + count + 1];
            temp3 = values[j + count2];
            temp4 = values[j +count2 + 1];

            adjacency_matrix[i][j] = GPSDistanceHarversine.gpsDistance(temp1, temp2, temp3, temp4);

            count2+=1;
            }

            count+=1;
            count2 = 0;
        }
    }

    System.out.println("The cities are visited as follows:");

    TspMain tspNearestNeighbour = new TspMain();

    tspNearestNeighbour.tsp(adjacency_matrix);
}

The nested distance checking loop is causing the issues. Can anyone see what could be screwing up?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
brainiac
  • 41
  • 4
  • On which lines are the errors occurring? – bwroga Apr 22 '14 at 15:41
  • Depending on changing the nested loop ranges (from 78, 79, 80) I get errors on varying instances of all the following: temp1 = values[i+count]; temp2 = values[i + count + 1]; temp3 = values[j + count2]; temp4 = values[j +count2 + 1]; adjacency_matrix[i][j] = GPSDistanceHarversine.gpsDistance(temp1, temp2, temp3, temp4); At the current loop set up which I believe is the correct one, it is giving an error at line 111 temp2 = values[i + count + 1]; – brainiac Apr 22 '14 at 15:54
  • 2
    Use meaningful variable names – stark Apr 22 '14 at 15:56
  • In this case, their only meaning is to pull the correct slot in the contents array out. Basically, my file has 160 lines, line 1 is the latitude of town1, line 2 is the longitude of town1 and so on down the line. The temp variables need to account for the jumps they need to make on each loop call to get the correct latitude and longitude for each respective town and then the haversine function calculates the distance from town x to y. Apologies for not making that clearer. – brainiac Apr 22 '14 at 16:02

0 Answers0