【问题标题】:Java - Cannot invoke padLz(int) on the primitive type intJava - 无法在原始类型 int 上调用 padLz(int)
【发布时间】:2014-03-04 01:35:39
【问题描述】:

我一直在尝试移植一段 JavaScript 代码,而现在,我还没有开始工作的唯一部分出现了这个错误:无法在原始类型 int 上调用 padLz(int) 。下面是原始的 JavaScript 代码,同样是我的移植版本,我还不能开始工作的部分突出显示。

错误:

无法在原始类型 int 上调用 padLz(int)

有人知道修复方法吗?

原创

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

移植版

  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));

字符串 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;
    }

【问题讨论】:

  • 原语没有方法。 padLz 是做什么的?
  • 我已经解释过了,上面放了代码

标签: java javascript android eclipse android-activity


【解决方案1】:

将原始 int 值作为参数传递给 padLz 函数。

作为

String padLz(int val, int digits) {

然后考虑使用

org.apache.commons.lang3.StringUtils.leftPad("" + val, digits, '0');

或在

中找到的变体

How can I pad an integers with zeros on the left?

示例

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

【讨论】:

  • 你能举个例子吗?以上不明白
  • 我添加了一个例子
【解决方案2】:

将 e.padLz(digits/2) 和 n.padLz(digits/2) 替换为 padLz(e, digits/2) 和 padLz(n, digits/2),然后实现一个带有签名的方法:

String padLz(int val, int digits) {
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-11
    • 2018-11-03
    • 2012-04-15
    • 2014-05-19
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多