gpt4 book ai didi

android - 无法解析为整数

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:16 25 4
gpt4 key购买 nike

好的...我有这个 .txt 文件 (UTF-8)

4661,SOMETHING,3858884120607,24,24.09
4659,SOMETHING1,3858884120621,24,15.95
4660,SOMETHING2,3858884120614,24,19.58

还有这段代码

FileInputStream fis = new FileInputStream(new File(someTextFile.txt));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader in = new BufferedReader(isr);

int i = 0;
String line;
while((line = in.readLine()) != null) {
Pattern p = Pattern.compile(",");
String[] article = p.split(line);

// I don't know why but when a first line starts with
// an integer - article[0] (which in .txt file is 4661)
// becomes someWeirdCharacter4661 so I need to trim it
// *weird character is like |=>|

if (i == 0) {
StringBuffer articleCode = new StringBuffer(article[0]);
articleCode.deleteCharAt(0);
article[0] = articleCode.toString();
}

SomeArticle**.addOrChange(mContext, Integer.parseInt(article[0]), article[1], article[2], Integer.parseInt(article[3]), Double.parseDouble(article[4]));

i++;
}

模拟器上没问题 但在真实设备 (HTC Desire) 上我得到这个(奇怪的)错误:

E/AndroidRuntime(16422): java.lang.NumberFormatException: unable to parse '4661' as integer

有什么问题?

** 这只是我的一些类需要这些参数作为输入(上下文、整数、字符串、字符串、整数、 double )

最佳答案

您的文件可能不是 UTF8 或类似的东西。

但是,如果您因为对问题不感兴趣而想破解修复程序,那么只需一个解决方案 :) 然后去掉任何不是数字或小数点的内容。

String[] article = p.split(line);
Integer i = Integer.parseInt(article[0].replaceAll("[^0-9.]",""));

正则表达式并不完美(它会影响 ...999.... 例如)但它会为你做。

编辑:

我似乎没有正确阅读问题。如果它只在文件的开头,那么很可能你拥有的是一个字节顺序标记,它用于告诉你文件是 unicode 还是 UTF16/32 是小端还是大端字节序。您不需要经常看到它被使用。

http://unicode.org/faq/utf_bom.html#bom10

关于android - 无法解析为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4599061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com