-2

Could somebody help me to figure out how to solve a weird (for me) issue. The idea is we get a short String from file (to be precise string is "117_63_", where "_" are space symbols and the file itself is UTF-8 encoded) we split this string into integers "117" and "63" but when we conver them using Inetger.parseint() method it returns exception ...

java.lang.NumberFormatException: For input string: "117"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at xor_cipher.get_crypted_ints_from_string(xor_cipher.java:79)
    at xor_cipher.decrypt(xor_cipher.java:49)
    at the_main.main(the_main.java:25)

main :

///////////////////////////////////
//                               //
//         Main entrance         //
//                               //
///////////////////////////////////
public static void main( String[ ] cmd_arguments ) {
    // Just for the testing
    File fl = new File( "account_database" );
    try 
    {
        Scanner Get = new Scanner( fl );
        String tmp = Get.nextLine( );
        tmp = xor_cipher.decrypt( tmp );
        Get.close( );
    }
    catch ( Exception EXC ) { 
        EXC.printStackTrace( ); 
        System.exit( 1 );
    }
}

function which invokes exception and is located in another class :

////////////////////////////////////
//                                //
//   Convert string of integers   //
//   to array of integers ...     //
//                                //
////////////////////////////////////
public static int[ ] get_crypted_ints_from_string( String crypted_ints_as_string ) {
    int amount = 0;

    for ( int i = 0; i < crypted_ints_as_string.length( ); i++ ) {
        if ( crypted_ints_as_string.charAt( i ) == ' ' ) amount++;
        else continue;
    }

    int array[ ] = new int[ amount ];
    int analyzed_border = 0;

    for ( int i = 0; i < array.length; i++ ) {
        String tmp = new String( );
        for ( int j = analyzed_border; j < crypted_ints_as_string.length( ); j++ ) {
            if ( crypted_ints_as_string.charAt( j ) == ' ' ) {
                array[ i ] = Integer.parseInt( tmp ); // <-- exception when tmp="112"
                analyzed_border = j + 1;
                break;
            }
            else if ( j == crypted_ints_as_string.length( ) - 1 ) {
                tmp += crypted_ints_as_string.charAt( j );
                array[ i ] = Integer.parseInt( tmp );
                analyzed_border = j + 1;
                break;
            }
            else tmp += crypted_ints_as_string.charAt( j );
        }
    }
    return array;
}

I had been "fighting" that for a long time already, i would appreciate if anyone could at least give me a tip ..

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
M.Smith
  • 19
  • 3
  • 4
    Did you step through the code in a debugger and see the value of tmp? If you did, you should see that it is an empty string, as it is never assigned any value. – OldProgrammer Mar 24 '18 at 16:05

2 Answers2

3

You have a hidden special character in your input :

Lets check your input what gives exactly, this "117" return (check this)

\u0022\ufeff\u0031\u0031\u0037\u0022
      ^^^^^^

but a normal input "117" return (check this)

\u0022\u0031\u0031\u0037\u0022

Note that your input contain a hidden character in the beginning \ufeff (ZERO WIDTH NO-BREAK SPACE) this Unicode makes this problem.

Unicode

Solve your problem

To solve your problem you can use replaceAll to replace all non digits from your input :

Integer.parseInt("117".replaceAll("\\D", ""))
Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

Your problem is, that you use Integer.parseInt() probably with an empty string.

for ( int i = 0; i < array.length; i++ ) {

    String tmp = new String( ); // << here you instantiate an empty String

    for ( int j = analyzed_border; j < crypted_ints_as_string.length( ); j++ ) {
        if ( crypted_ints_as_string.charAt( j ) == ' ' ) {

            // << up to here tmp never gets changed again. >>
            // << How do you prevent applying Integer.parseInt() on an empty String here? >>

            array[ i ] = Integer.parseInt( tmp ); // <-- exception when tmp="112"
            analyzed_border = j + 1;
            break;
        }

Are you sure that tmp definitely always has a value other than null or empty when you enter the upper if-condition?

  • The posted error message does say `For input string: "117"`, so it being as a result of an empty string seems unlikely. – Bernhard Barker Mar 24 '18 at 16:20
  • I totally agree, but until now there is no information provided that shows how and where the values were debugged. I addressed the first possible problem that came into my mind plainly reviewing the code. –  Mar 24 '18 at 16:23