Results 1 to 6 of 6
  1. #1
    HesRight1 is offline Member
    Join Date
    Mar 2014
    Posts
    3
    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

    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 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
    Java 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
    HesRight1 is offline Member
    Join Date
    Mar 2014
    Posts
    3
    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

    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 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
    Java 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;
        	}

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    HesRight1 is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Stuck on porting small segment of JavaScript to java

    Quote Originally Posted by Norm View Post
    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.
    The javascript function is used to convert numeric grid reference (in metres) to standard-form grid ref. The input is easting and northings. Use the link for more information

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Stuck on porting small segment of JavaScript to java

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Porting C++/C# to Java for OpenGL
    By bmcclint in forum New To Java
    Replies: 0
    Last Post: 05-29-2013, 07:05 PM
  2. Calculating segment costs
    By sezzy in forum New To Java
    Replies: 4
    Last Post: 11-16-2012, 02:32 PM
  3. Replies: 2
    Last Post: 03-17-2012, 07:30 PM
  4. simple porting (from c) problem
    By Kearnel in forum Advanced Java
    Replies: 5
    Last Post: 01-07-2011, 07:04 PM
  5. Porting to a Nokia Device
    By David55 in forum CLDC and MIDP
    Replies: 1
    Last Post: 06-30-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •