gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:42 26 4
gpt4 key购买 nike

我正在尝试获取一个字符串并将其转换为 long,但我不断收到上述错误

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

有什么想法吗?

最佳答案

你必须解析,只有一个有效的数字,空字符串""不是一个有效的数字。所以,在解析之前你必须检查它。否则,您将得到 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;
}

关于输入字符串 ""的 java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23234688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com