Results 1 to 6 of 6
 03032014, 09:11 PM #1Member
 Join Date
 Mar 2014
 Posts
 3
 Rep Power
 0
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
Convert between Latitude/Longitude & OS National Grid Reference points
Original JavaScript
Java 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; }
Java 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; }
 03032014, 09:12 PM #2Member
 Join Date
 Mar 2014
 Posts
 3
 Rep Power
 0
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
Convert between Latitude/Longitude & OS National Grid Reference points
Original JavaScript
Java 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; }
Java 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; }
 03032014, 09:22 PM #3
Re: Stuck on porting small segment of JavaScript to java
Threads merged.
Can you explain what the code is supposed to do? Can you give some examples of input and output?
Copy the full text of the compiler's error messages and paste it here.If you don't understand my response, don't ignore it, ask a question.
 03032014, 09:28 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,389
 Blog Entries
 7
 Rep Power
 25
Re: Stuck on porting small segment of JavaScript to java
The original function seems to return a String; your method is a void method, so all bets are off because it can't return anything at all. b.t.w. an empty String is "" in Java, and please read the API documentation for the String class so you can see which methods are available.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03032014, 09:38 PM #5Member
 Join Date
 Mar 2014
 Posts
 3
 Rep Power
 0
 03032014, 11:36 PM #6
Re: Stuck on porting small segment of JavaScript to java
Also posted at: Stuck on porting small segment of JavaScript to java  Dev Shed
If you don't understand my response, don't ignore it, ask a question.
Similar Threads

Porting C++/C# to Java for OpenGL
By bmcclint in forum New To JavaReplies: 0Last Post: 05292013, 07:05 PM 
Calculating segment costs
By sezzy in forum New To JavaReplies: 4Last Post: 11162012, 03:32 PM 
Stuck on driver porting C's Pointer > Java's Reference
By FlyNn in forum Advanced JavaReplies: 2Last Post: 03172012, 08:30 PM 
simple porting (from c) problem
By Kearnel in forum Advanced JavaReplies: 5Last Post: 01072011, 08:04 PM 
Porting to a Nokia Device
By David55 in forum CLDC and MIDPReplies: 1Last Post: 06302008, 01:44 PM
Bookmarks