无法解析为整数

发布于 2024-10-10 19:58:13 字数 1310 浏览 8 评论 0原文

好吧...我有这个 .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

What's the the问题?

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

Alright...I have this .txt file (UTF-8)

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

And this code

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++;
}

On emulator it's fine but on real device (HTC Desire) I get this (weird) error:

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

What's the problem?

** it's just some my class which needs those parameters as input (context,int,string,string,int,double)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征棹 2024-10-17 19:58:13

您的文件可能不是 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

It could that your file is not UTF8 or something along those lines.

However if you want to hack a fix because you are not interested in the problem just a solution :) then strip out anything that isn't a digit or decimal point.

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

The regular expression isn't perfect (it would affect ...999.... for example) but it will do for you.

EDIT:

I did not read the question properly it seems. If it is only at the start of the file then it is very likely that what you have is a byte order mark, which is used to tell you if the file is unicode and also in UTF16/32 whether it is is little endian or big endian. You don't need tend to see it used very often.

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

薄荷港 2024-10-17 19:58:13

我本来打算将其添加为评论,但决定也包含一张图片。问题似乎不在于该文件不是 UTF-8,但事实上恰恰相反 - 看起来它是 UTF-8,但没有正确读取。

该图像来自十六进制编辑器,查看我创建的包含第一行的 UTF-8 文件。请注意 4661 前面的 3 个字符...

alt text

如果我以 ANSI 格式保存文件,这些字符不存在。

I was going to add this as a comment but decided to include an image as well. It seems the problem is not that the file isn't UTF-8 but in fact the opposite is true - it seems it IS UTF-8 but it isn't being read correctly.

The image is from a hex editor looking at a UTF-8 file I created containing the first line. Note the 3 characters preceding 4661...

alt text

If I save the file in ANSI format, those characters aren't there.

谁把谁当真 2024-10-17 19:58:13

您可以使用Notepad++,打开文本文件,选择菜单编码-->“以UTF-8无BOM编码”并使用此选项保存。编码字节 (EF BB BF) 将被删除,因此您的代码可以毫无问题地将字符串解析为整数。

希望这有帮助。

You can use Notepad++, open your text file, choose menu Encoding-->"Encoding in UTF-8 without BOM" and save with this option. The encoded bytes (EF BB BF) will be removed, so your code can parse string to integer without any problem.

Hope this help.

煮茶煮酒煮时光 2024-10-17 19:58:13

我已将文件转换为 ascii 格式,并且在类似的应用程序中可以正确读取它。

I've converted the file to read into ascii format, and it was read correctly in a similar application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文