[英]How to convert String to long without NumberFormatException?
I want to append these long variables, so I turned them into Strings first.我想追加这些长变量,所以我先把它们变成了字符串。 When I return them back into a long it throws a NumberFormatException.
当我将它们返回到 long 时,它会抛出一个 NumberFormatException。
public long BLZ = 12345678l;
public long KNr = 1234567890l;
public String landCode = "1314";
long fedor = Long.parseLong(String.valueOf(BLZ) + String.valueOf(KNr) +
landCode + "00");
long BLZ = 12345678l;
long KNr = 1234567890l;
String landCode = "1314";
System.out.println(Long.MAX_VALUE);
System.out.println(String.valueOf(BLZ) + String.valueOf(KNr) + landCode + "00");
leads to the output导致输出
9223372036854775807
9223372036854775807
123456781234567890131400
123456781234567890131400
The number you try to parse in is bigger than a long
can handle.您尝试解析的数字大于
long
可以处理的数字。
Edit: You asked how to solve your problem.编辑:您询问如何解决您的问题。 In the comments of your question there is the theory that you want to calculate the checksum of an IBAN.
在您的问题的评论中,有一种理论是您要计算 IBAN 的校验和。 You can do that by using
java.math.BigInteger
:你可以通过使用
java.math.BigInteger
来做到这一点:
System.out.println(Long.MAX_VALUE);
long BLZ = 12345678l;
long KNr = 1234567890l;
String landCode = "1314";
String val = String.valueOf(BLZ) + String.valueOf(KNr) + landCode + "00";
System.out.println(val);
System.out.println(BigInteger.valueOf(98).subtract(new BigInteger(val).mod(BigInteger.valueOf(97))));
This leads to the following output:这导致以下输出:
9223372036854775807
9223372036854775807
123456781234567890131400
123456781234567890131400
87
87
Alternatively you can check the IBAN Documentation (it's german but that shouldn't be problem for you I suppose ;-) where in chapter 4 there is a description of a way to calculate the checksum if you're limited (like here).或者,您可以查看IBAN 文档(它是德语,但我想这对您来说应该不是问题;-) 在第 4 章中,如果您受到限制(如此处),则描述了一种计算校验和的方法。 You will still need
longs
to be able to implement that, because 9-digit numbers can exceed the range of an int
.您仍然需要
longs
才能实现它,因为 9 位数字可能超出int
的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.