Java.lang.NumberFormatException: Invalid int: "5"

I am trying to read a .txt file which is a CSV file that is located in the Assets folder on Android. The first line is the number of rows and columns of the file The rest consists of values, I use ";" to separate each other.

Here's the code causing this weird error:

 public static int[][] getMatrix(InputStream stream) throws Exception {

    BufferedReader br = new BufferedReader((new InputStreamReader(stream, "UTF-8")));
    String buffer;
    String[] current;
    int[][] marbles = null;

    //Reading the matrix from file

    if((buffer = br.readLine()) != null) {
        current = buffer.split(delimiter);
        if(current.length !=2){
            throw new Exception("File format is not respected");
        }

        marbles = new int[Integer.parseInt(current[0])][Integer.parseInt(current[1])];

        int count = 0;
        while ((buffer = br.readLine()) != null) {
            current = buffer.split(delimiter);

            for (int i=0;i<current.length;i++){
                marbles[count][i] = Integer.parseInt(current[i]);

            }
            count++;
        }
    }

    br.close();

    return marbles;
}

      

I read the file using the one I get from the getAssets () method as the InputStream. open ().

Here's the csv file:

5;11
1;1;2;1;1;2;1;1;1;2;-1
1;2;1;1;2;2;1;2;2;1;-1
2;2;1;2;1;2;2;1;1;2;-1
1;1;2;1;1;1;1;2;1;2;-1
2;2;1;2;2;1;2;1;1;1;-1

      

I am getting an error on the first line, but it clearly shows that the line that calls it is the correct "5".

Mistake:

java.lang.NumberFormatException: Invalid int: "5"

      

which is of course caused by a piece of code that tries to convert a string to an integer.

+3


source to share


3 answers


My wild guess is that your file contains the BOM at the beginning of the text stream, which is confusing your parser. You can use the command file

on * NIX system to check this.



Try replacing this first line with another and see if you get the same error with the first number on another line. If you have installed the spec, google "remove bom from utf-8" for further instructions.

+4


source


The only possible reason for this behavior is non-printable characters in your file. Have a look at this question: How do I replace non-printable Unicode characters in Java? and apply this function to all your values, i.e .:



marbles = new int[Integer.parseInt(current[0].replaceAll("\\p{C}", "?"))][Integer.parseInt(current[1].replaceAll("\\p{C}", "?"))]

      

+1


source


In my case, the problem is that the custom defined attribute has no assigned value.

<FrameLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="?attr/image_visibility">

</FrameLayout>

      

And this is the definition of a custom attribute:

<attr name="image_visibility">
    <enum name="visible" value="0"/>
    <enum name="invisible" value="1"/>
    <enum name="gone" value="2"/>
</attr>

      

The problem was that I hadn't assigned any value for this custom attribute.
Removing or assigning a value to this custom attribute will fix the error.

0


source







All Articles