user1060249
user1060249

Reputation: 11

How do you index a String variable?

how do you index aString variable. So far I have been trying to use:

   ...
   private GeoPoint points[];
   int counter = 0;
   ...
   counter ++;
   String[] RowData = line.split(",");
   longitude = RowData[0];
   latitude = RowData[1];
   Double lat = new Double(latitude);
   Double lng = new Double(longitude);
   points[counter] = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
   ...
   for(int i=0; i < pointsCounter-1; i++) {
   geopoint1 = points[i];
   geopoint2 = points[i+1];
   }
   ...

Every time I test the above it force closes - any ideas? Thanks.

Sorry for the unclear question - what I am actually trying to do is draw a path on a map overlay - I can get all the geoPoints which enable me to draw a very nice dotted line, ie. from point 'a to b', and then from 'c to d' and so on, so I am trying to find a way of recording the previous geopoint so I can use it to go from 'b to c'. I am trying to use the index of points by using the 'counter' int to do this.

If I don't try and index 'points' it all works fine but as mentioned I get a dotted line.

I hope this makes sense - I have been going round in circles for the past day or so, sorry if I come across as a bit of a numpty (although I probably am!).

Here is the code in more detail;

    try {
         String line;
        while ((line = reader.readLine()) != null) {

            counter ++;

        String[] RowData = line.split(",");
        longitude = RowData[0];
        latitude = RowData[1];

        Double lat = new Double(latitude);
        Double lng = new Double(longitude);

        points[counter] = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

        for(int i=0; i < pointsCounter-1; i++){

        geopoint1 = points[i];
                    geopoint2 = points[i+1];

        p1 = new Point();
        p2 = new Point();   
        path = new Path();

        Projection projection = mapv.getProjection();
        projection.toPixels(geopoint1, p1);
        projection.toPixels(geopoint2, p2);

        path.moveTo(p2.x, p2.y);
        path.lineTo(p1.x,p1.y);

        canvas.drawPath(path, mPaint);

        }

        }

    }
    catch (IOException ex) {
        // handle exception
    }

Many thanks in advance

Upvotes: 0

Views: 155

Answers (3)

user130076
user130076

Reputation:

If I understand you correctly you're getting an IndexOutOfBoundsException in your for loop over the points. This is because when you get to the last element the index i+1 is no longer valid, e.g.

for(int i=0; i < pointsCounter-1; i++){
    //when i == pointsCounter-1, i+1 == pointsCounter, which is not a valid index
    geopoint1 = points[i];
    geopoint2 = points[i+1];
    ...
}

Just perform a simple check:

for(int i=0; i < pointsCounter-1; i++){
    //when i == pointsCounter-1, i+1 == pointsCounter, which is not a valid index
    geopoint1 = points[i];
    if (i == pointsCounter-1) {

      //there is no second point, so do something special
    } else {
        geopoint2 = points[i+1];
    }
    ...
}

You can clear up this logic to make it do as you want.

Upvotes: 0

user130076
user130076

Reputation:

If you want a particular character of a String use charAt

String x = "abcde"
x.charAt(0) // 'a'
x.charAt(4) // 'b'

Note charAt returns a Character and not a String.

Upvotes: 1

quaylar
quaylar

Reputation: 2635

Your pointsCounter is probably counting up to points.length, isnt it? Which means that points[i+1] in your loop is accessing an array-index that does not exist - and this is your problem - get the loop right...

Upvotes: 0

Related Questions