2

I'm trying to make a simple algorithm that converts .obj files to data which my program can use.

This is the input String data example: "v 1.000000 -1.000000 -1.000000" "f 1 2 3 4"

file = new BufferedReader(new InputStreamReader(am.open("test.obj"))); //Loads the .obj
        String out = " ";
        try{
            while((out=file.readLine()) != null){ //Grabs a line from the file and checks so that it aint null
                int index = 1;
                int nextIndex = 1;
                if(out.startsWith("v")){Log.w("OBJs", out); //If line starts with "v", go ahead
                    while(index != out.lastIndexOf(" ")){ //checks so the index is not the last index of an empty space " "
                        nextIndex = out.indexOf(" ", index +1); //Looks for the next occurance of an empty space from +1 of current empty space
                        vertices.add(Float.valueOf(out.substring(index, nextIndex))); //Grabs the string in between the two spaces
                        index = nextIndex;// 
                        }
                    vertices.add(Float.valueOf(out.substring(index, out.length())));//Grabs the last variable from the file as the above while is now false
                    }

                int index2 = 1;
                int nextIndex2 = 1;
                if(out.startsWith("f")){Log.w("OBJs", out);// THis works exactly as above, gives error and i dunno why
                    while(index2 != out.lastIndexOf(" ")){
                        nextIndex2 = out.indexOf(" ", index2 +1);
                        edges.add(Integer.valueOf(out.substring(index2, nextIndex2)));
                        Log.w("OBJs", out.substring(index2, nextIndex2)); 
                        index2 = nextIndex2;

                    }
                    edges.add(Integer.valueOf(out.substring(index2, out.length())));
                }

            }
        }

This results in:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.stuffs.blodvite/my.stuffs.blodvite.MainActivity}: java.lang.NumberFormatException: Invalid int: " 1"

And this only happens in the second loop segment, where I'm trying to gather the face information. Gathering the vertices works flawlessly and I'm using the exact same algorithm for both so I don't understand what the problem is. Also something funny to note is that when I turned eclipse off last time, I think it did work, no idea what happened or if I just remember it wrong. Oh and, as you can see from the code it prints the line its reading to Log cat. This is what comes out.

01-26 23:42:13.916: W/OBJs(18442): v 1.000000 -1.000000 -1.000000
01-26 23:42:13.916: W/OBJs(18442): v 1.000000 -1.000000 1.000000
01-26 23:42:13.916: W/OBJs(18442): v -1.000000 -1.000000 1.000000
01-26 23:42:13.916: W/OBJs(18442): v -1.000000 -1.000000 -1.000000
01-26 23:42:13.916: W/OBJs(18442): v 1.000000 1.000000 -0.999999
01-26 23:42:13.916: W/OBJs(18442): v 0.999999 1.000000 1.000001
01-26 23:42:13.916: W/OBJs(18442): v -1.000000 1.000000 1.000000
01-26 23:42:13.916: W/OBJs(18442): v -1.000000 1.000000 -1.000000
01-26 23:42:13.916: W/OBJs(18442): f 1 2 3 4

So it clearly displays that the vertices gathering works without error, however the face gathering stops on its first iteration.

AShelly
  • 34,686
  • 15
  • 91
  • 152
user1295313
  • 65
  • 2
  • 11

1 Answers1

1

This happens because you have a leading space in " 1". As already pointed out by @Raghav Sood, you can call trim on your Strings before trying to parse to int to get it to work. You can also simply take the substring starting at index2+1 (i.e. skipping the first space).

I am curious why you didn't simply use StringTokenizer for this.

MAK
  • 26,140
  • 11
  • 55
  • 86