输入字符串“1”的java.lang.NumberFormatException。 [英] java.lang.NumberFormatException for input string "1"
问题描述
所以,我有一个真正困扰我的问题。我有一个简单的解析器,我在java中。以下是相关代码:
So, I have an issue that really bothers me. I have a simple parser that I made in java. Here is the piece of relevant code:
while( (line = br.readLine())!=null)
{
String splitted[] = line.split(SPLITTER);
int docNum = Integer.parseInt(splitted[0].trim());
//do something
}
输入文件是CSV文件,第一个该文件的输入是一个整数。当我开始解析时,我会立即得到这个异常:
Input file is CSV file, the first entry of the file being an integer. When I start parsing, I immidiately get this exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at dipl.parser.TableParser.parse(TableParser.java:50)
at dipl.parser.DocumentParser.main(DocumentParser.java:87)
我检查了文件,确实有1作为第一个值(该字段中没有其他字符) ,但我仍然得到消息。我认为可能是因为文件编码:它是UTF-8,带有Unix端线。该程序在Ubuntu 14.04上运行。欢迎任何寻找问题的建议。
I checked the file, it indeed has 1 as its first value (no other characters are in that field), but I still get the message. I think that it may be because of file encoding: it is UTF-8, with Unix endlines. And the program is run on Ubuntu 14.04. Any suggestions where to look for the problem are welcome.
推荐答案
你有一个 BOM ;如果我在你的问题中复制了类似1
的内容并将其粘贴到 vim
中,我看到你有一个FE FF(例如, BOM )就在它前面。从该链接:
You have a BOM in front of that number; if I copy what looks like "1"
in your question and paste it into vim
, I see that you have a FE FF (e.g., a BOM) in front of it. From that link:
构成BOM的确切字节将是通过该转换格式转换为Unicode字符U + FEFF的任何内容。 / p>
The exact bytes comprising the BOM will be whatever the Unicode character U+FEFF is converted into by that transformation format.
这就是问题所在,使用适当的读取器使用该文件进行转换(UTF-8,UTF-16 big-endian,UTF) -16 little-endian等)文件用。有关使用Java读取Unicode文件的更多信息,另请参阅此问题及其答案。
So that's the issue, consume the file with the appropriate reader for the transformation (UTF-8, UTF-16 big-endian, UTF-16 little-endian, etc.) the file is encoded with. See also this question and its answers for more about reading Unicode files in Java.
这篇关于输入字符串“1”的java.lang.NumberFormatException。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!