3

I try to read a .txt file which is basically a CSV file which is located in the Assets folder in Android. In the first row there is the number of row and columns of the file The rest is composed by the values, i use a ";" to separate each other.

Here's the code which causes this strange 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 a file using as InputStream the one i get from the getAssets().open() method.

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 get the error in the first line, but it clearly shows that the string which causes it is a proper "5".

Error:

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

which is of course caused by the portion of code which tries to convert the string to an integer.

  • Side note on code quality: you should really really break up your method. It is way too long; dealing with all kinds of (different) abstraction levels and simply doing **many** things instead of **one** thing. That would also make it much easier to understand what is going on; to isolate the bug and find a fix more quickly ;-) And if it wouldnt be static, it might even be possible to write a set of helpful unit tests for your code; that also helps with preventing the "hey, I have written tons and tons of code; and surprise; it aint working" trap the next time. – GhostCat Jul 29 '15 at 13:34
  • 2
    @Marco can you post the full stack trace/ – Jishnu Prathap Jul 29 '15 at 13:36
  • 6
    Have you checked the CSV for non-printing characters? – Samuel Jul 29 '15 at 13:38
  • We need to see the **full** stack trace, and we need to know what version of Android this is. Please [edit] your post with this information. – durron597 Jul 29 '15 at 13:41
  • 3
    I just copied your code into a test with the provided CSV, and I was unable to reproduce the exception. Granted it wasn't in Android, but unless this is some glaring issue in the Android SDK, I feel like there is something missing... – Samuel Jul 29 '15 at 13:43
  • @Jägermeister thank you, i'm a little concerned about how to split the method: it simply reads a text file ant puts the content into a matrix, which is basically a single thing. Also, it doesn't need the class to be created, i just needed an utility class to read the files i needed, that's why it's static. This class is also already tested through junit and the reason why i opened this post is because there are other files i use to test it and they work perfectly fine. – Marco Dagrada Jul 29 '15 at 14:19
  • Well, the method does ... does initial checking ... then "parsing" of lines ... creating certain output. Those are things that represent "different" activities. The point of clean code in this case (in my eyes): a class/method should have one responsibility - meaning: one reason to change. – GhostCat Jul 29 '15 at 14:37

3 Answers3

4

My wild guess is that your file contains a BOM at the beginning of its text stream which is confusing your parser. You can use the file command on a *NIX system to verify this.

Try swapping that first line with another and see if you get the same error with the first number on another line. If you were set up the BOM, google "removing bom from utf-8" for further instructions.

  • Reproduces the same message that OP is getting. I think you're right. – Samuel Jul 29 '15 at 13:55
  • 1
    Thanks A LOT, i was using notepad++ to edit my csv file and although i activated the option to show all symbols i couldn't figure out the reason why it didn't work. Then i discovered the "save without BOM" option and everything is finally fine. – Marco Dagrada Jul 29 '15 at 13:58
1

The only one possible cause for this behavior is unprintable symbols in your file. Look at this question: How can I replace non-printable Unicode characters in Java? and apply given function to all your values, i.e.:

marbles = new int[Integer.parseInt(current[0].replaceAll("\\p{C}", "?"))][Integer.parseInt(current[1].replaceAll("\\p{C}", "?"))]
Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

In my case the problem that a custom defined attribute without any value assigned.

<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 custom attribute definition:

<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 didn't assign any value for this custom attribute.
Removing that custom attribute or assigning it a value will fix the error.

vovahost
  • 34,185
  • 17
  • 113
  • 116