你说我漏掉了什么?NumberFormatException 错误

3
我想读取一个只包含数字的txt文件。这个文件是UTF-8编码,数字之间只有换行符分隔(没有空格或其他任何字符)。每当我调用Integer.valueOf(myString)时,就会出现异常。
这个异常非常奇怪,因为如果我创建一个预定义的字符串,比如"56\n",并使用.trim()方法,它就可以完美地工作。但在我的代码中,情况不仅如此,而且异常文本显示无法转换的是"54856"。我尝试在那里加入一个换行符,然后错误文本显示无法转换的是"54856\n"。
除此之外,我还缺少什么?
File ficheroEntrada = new File("C:\\in.txt");
FileReader entrada =new FileReader(ficheroEntrada);
BufferedReader input = new BufferedReader(entrada);

String s = input.readLine();
System.out.println(s);
Integer in;
in = Integer.valueOf(s.trim());
System.out.println(in);

异常信息如下所示:
Exception in thread "main" java.lang.NumberFormatException: For input string: "54856"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:658)
    at java.base/java.lang.Integer.valueOf(Integer.java:989)
    at Quicksort.main(Quicksort.java:170)

文件in.txt包含以下内容:

54856
896
54
53
2
5634

如果是在Windows下,你的字符串中也可能会包含换行符('\r')。 - Neijwiert
2
隐藏字符,也许?尝试使用Arrays.toString从字符串中打印出字符数组。 - RealSkeptic
input是什么类型的对象?你能展示一下创建它的代码行吗? - lealceldeiro
请问您能否检查字符串中字符的序号?它们可能是ASCII范围之外的Unicode字符吗?0-9通常应该在ASCII范围内,但也许这些不是默认的0-9字符。 - TreffnonX
可以包含打印输出,例如 System.out.println(".." + s + "..");,这可能会有所帮助。奇怪的是,trim函数不会删除\r - matt
请查看更新@AlbertoSantosAraus。 - MS90
4个回答

2

显然,这是与Windows和它使用的\r有关的问题...我刚刚尝试在Linux VM上执行它,它可以工作。感谢所有回答的人!!


看看我的更新代码,Alberto,也在Windows上试一下。@AlbertoSantosAraus - MS90

1
尝试使用Scanner类读取文件,并使用其hasNextInt()方法来确定所读内容是否为Integer。这将帮助您找出导致问题的字符串/字符是什么。
public static void main(String[] args) throws Exception {
    File ficheroEntrada = new File(
            "C:\\in.txt");
    Scanner scan = new Scanner(ficheroEntrada);
    while (scan.hasNext()) {
        if (scan.hasNextInt()) {
            System.out.println("found integer" + scan.nextInt());
        } else {
            System.out.println("not integer" + scan.next());
        }
    }
}

0

很可能您的输入中有前导/尾随空格,例如:

String s = " 5436";
System.out.println(s);
Integer in;
in = Integer.valueOf(s.trim());
System.out.println(in);

使用 trim() 函数来去除字符串中的空格。

UPDATE 2:

如果您的文件包含以下内容:
54856\n
896
54\n
53
2\n
5634

然后使用以下代码:

  ....your code
    FileReader enter = new FileReader(file);
    BufferedReader input = new BufferedReader(enter);
    String currentLine;
    while ((currentLine = input.readLine()) != null) {
    Integer in;
    //get rid of non-numbers
    in = Integer.valueOf(currentLine.replaceAll("\\D+",""));
    System.out.println(in);
    ...your code

是的,我使用了 trim 函数来指定,但错误仍然存在。这就让我感到困惑。 - Alberto Santos Araus
你能展示一下你的输入文件内容吗?@AlbertoSantosAraus - MS90

0

如果你想确保一个字符串可解析,你可以使用 Pattern 和 Regex。

Pattern intPattern = Pattern.compile("\\-?\\d+");
Matcher matcher = intPattern.matcher(input);
if (matcher.find()) {
    int value = Integer.parseInt(matcher.group(0));
    // ... do something with the result.
} else {
    // ... handle unparsable line.
}

这个模式允许任何数字,并且可以在前面加上负号(没有空格)。它应该能够解析,除非它太长了。我不知道它如何处理这种情况,但是你的示例似乎主要包含短整数,所以这应该不重要。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接