无法解析为整数整数

2023-09-06 02:28:13 作者:绝配

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

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

这code

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

在模拟器它的罚款的,但在实际设备(HTC Desire的)我得到这样的(奇怪的)错误:

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

什么问题?

**这只是它需要这些参数作为输入(背景下,INT,字符串,字符串,整型,双)我的一些类

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

推荐答案

这可能是你的文件是不是UTF8或类似的规定。

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.]",""));

常规EX pression是不完美的(这会影响... 999 ....为例),但它会为你做。

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

编辑:

我没有看过这个问题妥善看来。如果只是在文件的开头那么它很有可能是你所拥有的是一个字节顺序标记,这是用来告诉你,如果该文件是单向code,并在UTF16 / 32无论是在小端或大端。你不需要往往看到它使用非常频繁。

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://uni$c$c.org/faq/utf_bom.html#bom10

 
精彩推荐
图片推荐