java.lang.NumberFormatException:对于输入字符串:java.lang.NumberFormatException.forInputString(未知来源)处的“52586380‬”

问题描述 投票:1回答:1

我正在编写一个简单的函数,该函数接受字符串-2739379824/2752586380‬/2286078538,将其拆分为3个字符串,并将每个字符串转换为Big Integer。问题是第一个和第三个字符串的转换成功,但是对于第二个字符串,我收到以下错误:价值观-27393798242752586380‬2286078538a:-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) ;
        }
java talend numberformatexception
1个回答
0
投票

请勿复制粘贴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;
}
© www.soinside.com 2019 - 2024. All rights reserved.