Question

I'm fairly good at Java but somewhat at a loss when the some of the JavaScript below is mentioned. I've attempted to port the below code but I'm finding it somewhat difficult. Any help would be great :-)

Parts of JavaScript I'm stuck at porting:

  1. I'm stuck on the return statement on the 3rd line.
  2. Similarly the lines utilizing "charCodeAt", as I keep getting error "Cannot invoke charCodeAt(int) on the primitive type char"
  3. Also on the second to last line, I keep getting the error "Cannot invoke padLZ(int) on the primitive type int".

Link to original javascript

http://www.movable-type.co.uk/scripts/latlong-gridref-v1.html

Original JavaScript

 OsGridRef.prototype.toString = function(digits) {
              digits = (typeof digits == 'undefined') ? 10 : digits;
              e = this.easting, n = this.northing;
              if (e==NaN || n==NaN) return '??';

              // get the 100km-grid indices
              var e100k = Math.floor(e/100000), n100k = Math.floor(n/100000);

              if (e100k<0 || e100k>6 || n100k<0 || n100k>12) return '';

              // translate those into numeric equivalents of the grid letters
              var l1 = (19-n100k) - (19-n100k)%5 + Math.floor((e100k+10)/5);
              var l2 = (19-n100k)*5%25 + e100k%5;

              // compensate for skipped 'I' and build grid letter-pairs
              if (l1 > 7) l1++;
              if (l2 > 7) l2++;
              var letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));

              // strip 100km-grid indices from easting & northing, and reduce precision
              e = Math.floor((e%100000)/Math.pow(10,5-digits/2));
              n = Math.floor((n%100000)/Math.pow(10,5-digits/2));

              var gridRef = letPair + ' ' + e.padLz(digits/2) + ' ' + n.padLz(digits/2);

              return gridRef;
            }

Attempted Port

  public void gridrefNumToLet(int e, int n, int digits) {
      // get the 100km-grid indices
      double e100k = Math.floor(e/100000), n100k = Math.floor(n/100000);

      if (e100k<0 || e100k>6 || n100k<0 || n100k>12) return '';

      // translate those into numeric equivalents of the grid letters
      double l1 = (19-n100k) - (19-n100k)%5 + Math.floor((e100k+10)/5);
      double l2 = (19-n100k)*5%25 + e100k%5;

      // compensate for skipped 'I' and build grid letter-pairs
      if (l1 > 7) l1++;
      if (l2 > 7) l2++;
      double letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));

      // strip 100km-grid indices from easting & northing, and reduce precision
      e = Math.floor((e%100000)/Math.pow(10,5-digits/2));
      n = Math.floor((n%100000)/Math.pow(10,5-digits/2));

      double gridRef = letPair + e.padLZ(digits/2) + n.padLZ(digits/2);

      return gridRef;
    }

No correct solution

OTHER TIPS

Ok, a few things:

1 void is not the proper return type, it should be a String (it would be a double, but once you pad it with zeros, then it's a string:

public String gridrefNumToLet(int e, int n, int digits) 

2 You can return a single quote with nothing in it. so change line three to null:

if (e100k<0 || e100k>6 || n100k<0 || n100k>12) return null;

3 I don't know what this is supposed to do, but you're assigning a String to a double variable, so it needs to be converted to double if you're going to use it:

double letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));

4 padLZ is not a method in Java. You'll need to use a String formatter to do that:

String pattern = (I am not sure how many digits you'll need, but you can find lots of resources on string padding.)

return String.format(pattern, gridref);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top