将1000000000000001转换为base 5时的Java NumberFormatException

sun*_*985 2 java numberformatexception

我试图使用Java将字符串"1000000000000001"转换为基数5:

Integer number = Integer.parseInt("1000000000000001", 5);

但是,我是gettig NumberFormatException.该字符串被修剪,只包含1和0.有人可以解释为什么我得到这个例外?

Tun*_*aki 6

数量1000000000000001在基地5相当于数30517578126以10为基数(你可以验证这一点自己或使用在线工具).

但是,30,517,578,126太大而不适合int价值.最大值Integer.MAX_VALUE2,147,483,647.这解释了您获得的例外情况 - 来自parseInt:

throws NumberFormatException- 如果String不包含可解析的int.

这是这种情况.

你需要使用long:

public static void main(String[] args) {
    long number = Long.parseLong("1000000000000001", 5);
    System.out.println(number); // prints "30517578126"
}
Run Code Online (Sandbox Code Playgroud)