输入字符串""的java.lang.NumberFormatException;

时间:2014-04-23 04:22:57

标签: java long-integer numberformatexception

我正在尝试取一个字符串并将其转换为很长时间,以至于我一直得到上​​面提到的错误

public long[] stringToLongDecrypt()
{
    long ciphertext[] = new long[elements.length];
    for(int i=0; i<ciphertext.length; i++)
    {
        ciphertext[i] = Long.parseLong(elements[i].trim(), 16);
    }
    return ciphertext;
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您必须解析,只有有效数字,空字符串""不是有效数字。因此,您必须在解析之前检查它。否则,您将获得NumberFormatException

public long[] stringToLongDecrypt() {
    long ciphertext[] = new long[elements.length];
    for(int i=0; i<ciphertext.length; i++) {
       if(elements[i] != null && !elements[i].trim().isEmpty()) {
          ciphertext[i] = Long.parseLong(elements[i].trim(), 16);
       }
    }
    return ciphertext;
}

答案 1 :(得分:0)

Long无法解析String数据类型的输入。 请尝试使用以下内容:

 Long.valueOf(elements[i].trim()).longValue();