DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2006

    Error with strings

    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:
    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);
           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;
    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: -13

    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?


  2. #2
    Join Date
    Apr 2006
    no worries, have figured it out now!!

Similar Threads

  1. Replies: 5
    Last Post: 06-19-2006, 09:48 AM
  2. Comparing Strings in Array Part 2
    By triley35 in forum .NET
    Replies: 3
    Last Post: 04-05-2006, 11:10 AM
  3. How to break down a line into strings.
    By Valleriani in forum .NET
    Replies: 2
    Last Post: 10-30-2005, 10:38 PM
  4. PostBack while tracking Query strings
    By kashif_82 in forum ASP.NET
    Replies: 1
    Last Post: 07-26-2005, 08:23 AM
  5. Allocating memory in VB (Custom strings)
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 4
    Last Post: 03-04-2002, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.