gpt4 book ai didi

java - BigInteger 抛出 numberFormatException

转载 作者:行者123 更新时间:2023-12-02 01:02:49 24 4
gpt4 key购买 nike

编辑:使用以下代码解决了问题:

    String tokenId="0x1800000000001289000000000000000000000000000000000000000000000000";

BigInteger token1;
if (tokenId.startsWith("0x")){
token1=new BigInteger(tokenId.substring(2),16);

我有一个很长的字符串,我需要将其分配为 BigInteger 并将其传递给 Web3j 库的另一个方法。但是,我不断收到数字格式异常。对此有什么帮助吗?

下面是抛出异常的方法:

public void getBalance1155(String walletAddress) throws ExecutionException, InterruptedException {

//define constant values

Web3j web3j=Web3j.build(new HttpService("https://mainnet.infura.io/v3/<apiKey>>"));
String contractAddress = "0xfaaFDc07907ff5120a76b34b731b278c38d6043C";
BigInteger tokenId=new BigInteger("0x1800000000001289000000000000000000000000000000000000000000000000",16);
NoOpProcessor processor = new NoOpProcessor(web3j);
Credentials credentials = Credentials.create("privatekey");
TransactionManager txManager = new FastRawTransactionManager(web3j, credentials, processor);

//Query Blockchain to get balance of WALLETADDRESS from Contract for given TokenID

ERC1155 token = ERC1155.load(contractAddress, web3j, txManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT);
RemoteCall<BigInteger> sendCall = token.balanceOf(walletAddress, tokenId);
BigInteger balance=sendCall.sendAsync().get();
log.info("balance >>>>>> " +balance);
}

这是异常(exception):

java.lang.NumberFormatException: For input string: "0x1800000000001289000000000000000000000000000000000000000000000000" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Long.parseLong(Long.java:692) at java.base/java.lang.Long.parseLong(Long.java:817)

最佳答案

从字符串中删除无关的 0x

BigInteger's constructor 的文档说

The String representation consists of an optional minus or plus sign followed by a sequence of one or more digits in the specified radix.

[...] The String may not contain any extraneous characters

没有提及像0x(或八进制的0)这样的前缀。

关于java - BigInteger 抛出 numberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60449075/

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