#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Question 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.movable-type.co.uk/scripts/latlong-gridref-v1.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 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
    Code:
          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;
        	}
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    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?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0
    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
    fromCharCode
    What should I do to fix?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    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?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0
    The overall purpose of the code i'm attempting to port is to:
    Convert numeric grid reference (in meters) to standard-form grid ref


    It's explained further in the link on the first post
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    You can use the String.valueOf() method to convert a character to a String.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Question


    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"
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    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.

IMN logo majestic logo threadwatch logo seochat tools logo