我正在编写一个简单的函数,该函数接受字符串-2739379824/2752586380/2286078538
,将其拆分为3个字符串,并将每个字符串转换为Big Integer。问题是第一个和第三个字符串的转换成功,但是对于第二个字符串,我收到以下错误:价值观-2739379824
2752586380
2286078538
a:-2739379824
Exception in component tJava_1 (Montant)
java.lang.NumberFormatException: For input string: "52586380"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.math.BigInteger.<init>(Unknown Source)
at java.math.BigInteger.<init>(Unknown Source)
我尝试多次单独转换第二个字符串,有时我得到正确的输出,有时出现此错误,我不明白为什么。这是我的功能:
public static BigDecimal myfunction (String Value) {
String[] Valuestring = Value.split("/");
System.out.println("Values ");
System.out.println(Valuestring[0]);
System.out.println(Valuestring[1]);
System.out.println(Valuestring[2]);
BigInteger a = new BigInteger(Valuestring[0]) ;
System.out.print("a :") ;
System.out.println(a) ;
BigInteger b = new BigInteger(Valuestring[1]) ;
System.out.println("b :") ;
System.out.println(b) ;
BigInteger c = new BigInteger(Valuestring[2]) ;
System.out.println("c :") ;
System.out.println(c) ;
}
请勿复制粘贴String值,其中可能隐藏了特殊字符。我尝试了您的代码,它给了我您所说的错误。然后,我尝试删除/
并自己输入,这行得通。可能是复制粘贴源中隐藏了特殊字符。并在\\
中使用split()
public static void main( String[] args ) throws Exception
{
String x = "-2739379824/2752586380/2286078538"; // your string as it is giving the error
Test.myfunction( "-2739379824/2752586380/2286078538" ); // I tried deleting the / and typed it again
}
public static BigDecimal myfunction( String Value )
{
String[] Valuestring = Value.strip().split( "\\/" );
System.out.println( "Values " );
System.out.println( Valuestring[0] );
System.out.println( Valuestring[1] );
System.out.println( Valuestring[2] );
BigInteger a = new BigInteger( Valuestring[0].strip() );
System.out.print( "a :" );
System.out.println( a );
BigInteger b = new BigInteger( Valuestring[1].strip() );
System.out.println( "b :" );
System.out.println( b );
BigInteger c = new BigInteger( Valuestring[2].strip() );
System.out.println( "c :" );
System.out.println( c );
return null;
}