2

.obj ファイルをプログラムで使用できるデータに変換する単純なアルゴリズムを作成しようとしています。

入力文字列データの例: "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())));
                }

            }
        }

これにより、次の結果が得られます。

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

これは、顔情報を収集しようとしている 2 番目のループ セグメントでのみ発生します。頂点の収集は問題なく機能し、両方にまったく同じアルゴリズムを使用しているため、何が問題なのかわかりません。また、面白いことに、前回Eclipseをオフにしたとき、うまくいったと思いますが、何が起こったのか、または覚えていないだけなのかわかりません。ああ、コードからわかるように、その行を Log cat に出力します。出てくるのはこれ。

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

したがって、頂点の収集がエラーなしで機能することが明確に示されていますが、面の収集は最初の反復で停止します。

4

1 に答える 1

1

これは、" 1" の先頭にスペースがあるために発生します。@Raghav Sood ですでに指摘されているように、int に解析して機能させる前に、文字列でトリムを呼び出すことができます。index2+1 から始まる部分文字列を取得することもできます (つまり、最初のスペースをスキップします)。

これに単純に StringTokenizer を使用しなかった理由が知りたいです。

于 2013-01-28T19:36:00.853 に答える