I've been attempting to port a section of JavaScript code and now, the only section I'm still yet to get working has this error: Cannot invoke padLz(int) on the primitive type int . The original JavaScript code is below and similarly is my ported version, with the section that I've not yet been able to get working yet highlighted.
Error:
Cannot invoke padLz(int) on the primitive type int
Original
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;
}
// pad a number with sufficient leading zeros to make it w chars wide
Number.prototype.padLZ = function(w) {
var n = this.toString();
for (var i=0; i<w-n.length; i++) n = '0' + n;
return n;
}
Ported Version
public String 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 null;
// 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++;
String letPair = String.valueOf(l1+"A".charAt(0))+ String.valueOf(l2+"A".charAt(0));
// strip 100km-grid indices from easting & northing, and reduce precision
e = (int) Math.floor((e%100000)/Math.pow(10,5-digits/2));
n = (int) Math.floor((n%100000)/Math.pow(10,5-digits/2));
String gridRef = letPair + ' ' + e.padLz(digits/2) + ' ' + n.padLz(digits/2);
return gridRef;
}
// pad a number with sufficient leading zeros to make it w chars wide
public String padLZ(String w) {
String n = this.toString();
for (int i=0; i< n.length(); i++) n = '0' + n;
return n;
}
Replace e.padLz(digits/2) and n.padLz(digits/2) with padLz(e, digits/2) and padLz(n, digits/2), and then implement a method with a signature:
String padLz(int val, int digits) {
...
}
Pass your primitive int value into the padLz
function as a parameter.
as
String padLz(int val, int digits) {
then consider using
org.apache.commons.lang3.StringUtils.leftPad("" + val, digits, '0');
or variations as found in
How can I pad an integers with zeros on the left?
Example
private static String padLz (int val, int digits) {
return org.apache.commons.lang3.StringUtils.leftPad("" + val, digits, '0');
}
System.out.println(padLz (2, 5)); --> 00002
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.