I've got this method that takes a location of one format and then converts it to another. i'm getting an error but cant see why:
what i'm trying to do is split the data at the . and then convert it. it all compiles ok, but at runtime i get an error saying java.lang.StringIndexOutOfBoundsException: String index out of range: -13Code:public double convert (String data){ // example data is "5125.85111 N" splitData = new String[2]; String xs; String ys; int i, length, j=12, post=0; double x, y, z, result; double ten=10, power; char direction; for(i=0; i<2; i++) { post = data.indexOf(".", j); //split the data in two at the dot splitData[i] = data.substring(j, post); j=post+1; } xs = splitData[0].substring(0, 4); ys = splitData[0].substring(4); x = Double.parseDouble(xs); y = Double.parseDouble(ys); length = splitData[1].length(); direction = splitData[1].charAt(length); StringBuffer sb = new StringBuffer(splitData[1]); sb.deleteCharAt(length); //the string needs to be put in a buffer to remove the last element splitData[1] = sb.toString(); z = Double.parseDouble(splitData[1]); power = length - 1; z = z / power(ten, power); //change z to 0.z y = (y+z)/60; result = x+y; return result; }
my minimal debugging skills have found that this is something to do with the line splitData[i] = data.substring(j, post); and that post is at -1 at this point. can anyone tell me what's going on?
thanks


Reply With Quote


Bookmarks