0

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

5
  • 1
    Force closes on what line ? What's the exception ? Commented Feb 7, 2012 at 13:40
  • First of all: I should enclose all the code in a try/catch statement and log the catched Exception. Commented Feb 7, 2012 at 13:41
  • maybe this will help: docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Feb 7, 2012 at 13:42
  • What do you mean by "index"? You are not indexing anything on your code, you are splitting a String. Also, proper java formatting would use camelCase String[] rowData = .... Commented Feb 7, 2012 at 13:45
  • What size did you initialize the points[] array to? I don't see any sign of it in the posted code Commented Feb 7, 2012 at 14:10

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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...

Comments

0

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.

1 Comment

Thanks Peter - my problem hasn't been the for loop, it has been the fact that every time I try and use (points[counter] = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));) it force closes. If I use (points = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));) it works so it seems to be the fact that I am indexing the variable 'points' that is causing the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.