【问题标题】:NumberFormatException: Invalid int: " 1"NumberFormatException:无效的 int:“1”
【发布时间】:2013-01-11 23:59:14
【问题描述】:

我正在尝试制作一个简单的算法,将 .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"

这只发生在第二个循环片段中,我正在尝试收集面部信息。收集顶点完美无缺,我对两者都使用完全相同的算法,所以我不明白问题是什么。 另外有趣的是,当我上次关闭 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

因此它清楚地表明顶点收集工作没有错误,但是面收集在第一次迭代时停止。

【问题讨论】:

  • 在将字符串解析为整数之前尝试调用 trim()。
  • 您是否尝试在转换前trim()(以消除空格)?
  • 成功了! awsm,谢谢大家! :D 仍然很奇怪,为什么它的行为与上面的相同。

标签: java android algorithm loops .obj


【解决方案1】:

发生这种情况是因为您在“1”中有一个前导空格。正如@Raghav Sood 已经指出的那样,您可以在尝试解析为 int 以使其正常工作之前,在您的字符串上调用 trim 。您也可以简单地获取从 index2+1 开始的子字符串(即跳过第一个空格)。

我很好奇你为什么不简单地使用 StringTokenizer。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2012-06-29
    相关资源
    最近更新 更多