March 3rd, 2014, 02:11 PM

Stuck on porting small segment of JavaScript to java
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.movabletype.co.uk/scripts/latlonggridrefv1.html
Original JavaScript
Code:
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 100kmgrid 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 = (19n100k)  (19n100k)%5 + Math.floor((e100k+10)/5);
var l2 = (19n100k)*5%25 + e100k%5;
// compensate for skipped 'I' and build grid letterpairs
if (l1 > 7) l1++;
if (l2 > 7) l2++;
var letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));
// strip 100kmgrid indices from easting & northing, and reduce precision
e = Math.floor((e%100000)/Math.pow(10,5digits/2));
n = Math.floor((n%100000)/Math.pow(10,5digits/2));
var gridRef = letPair + ' ' + e.padLz(digits/2) + ' ' + n.padLz(digits/2);
return gridRef;
}
Attempted Port
Code:
public void gridrefNumToLet(int e, int n, int digits) {
// get the 100kmgrid 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 = (19n100k)  (19n100k)%5 + Math.floor((e100k+10)/5);
double l2 = (19n100k)*5%25 + e100k%5;
// compensate for skipped 'I' and build grid letterpairs
if (l1 > 7) l1++;
if (l2 > 7) l2++;
double letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));
// strip 100kmgrid indices from easting & northing, and reduce precision
e = Math.floor((e%100000)/Math.pow(10,5digits/2));
n = Math.floor((n%100000)/Math.pow(10,5digits/2));
double gridRef = letPair + e.padLZ(digits/2) + n.padLZ(digits/2);
return gridRef;
}
March 3rd, 2014, 03:31 PM

Code:
double letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));
Note that there isn't a fromCharCode in String. The idea behind String.fromCharCode in Javascript is to build a String giving the ASCII code for each character. Then it will form a single String. Why are you trying to convert it to a double?
March 3rd, 2014, 06:10 PM

Originally Posted by bullet
Code:
double letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));
Note that there isn't a fromCharCode in String. The idea behind String.fromCharCode in Javascript is to build a String giving the ASCII code for each character. Then it will form a single String. Why are you trying to convert it to a double?
My username was previously BlueHorn2 (forgot my password then email used to register began to play up. So I've created this new one).
I've changed
[code=java]
double letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));[/code]
to
[code=java] String letPair = String.fromCharCode(l1+"A".charAt(0), l2+"A".charAt(0));[/code]
But now I'm getting the error
[code=java]The method fromCharCode(double, double) is undefined for the type String[/code]
around
What should I do to fix?
March 3rd, 2014, 06:38 PM

What should that method do with the args it receives? What is it supposed to return?
Can you give an example of its args and what it returns?
March 3rd, 2014, 06:54 PM

The overall purpose of the code i'm attempting to port is to:
Convert numeric grid reference (in meters) to standardform grid ref
It's explained further in the link on the first post
March 3rd, 2014, 07:59 PM

You can use the String.valueOf() method to convert a character to a String.
March 3rd, 2014, 08:03 PM

Originally Posted by bullet
You can use the String.valueOf() method to convert a character to a String.
Works perfectly mate :) Thank you.
Do you have any idea how to get padLz() to work in java? I've scoured the internet and still don't know how.
I just keep getting the error "Cannot invoke padLz(int) on the primitive type int"
March 4th, 2014, 11:06 AM

You can't call methods on a primitive.
What you need to do is look at the documentation of padLZ and see what it does. Then you can look for something in Java that will do that.