我正在寻求一些帮助。我做了很多谷歌搜索,但收效甚微。
我是编程新手,我相信这对我来说是一个愚蠢的疏忽。
以下代码旨在读取以制表符分隔的.txt文档。 .txt文件格式为六列。目前,当我浏览文件时,我将字符串解析为适当的值类型并将其分配给其各自的变量。代码在这里:
try {
s = new Scanner(new BufferedReader(new FileReader(file))).useDelimiter("\t");
while (s.hasNextLine()) {
group = Integer.parseInt(s.next());
death = Integer.parseInt(s.next());
name = s.next();
sex = s.next();
age = Integer.parseInt(s.next());
fare = Double.parseDouble(s.next());
System.out.println("Class = " + group); // Test that value is being assigned
System.out.println("Death = " + death); // Test that value is being assigned
System.out.println("Name = " + name); // Test that value is being assigned
System.out.println("Gender = " + sex); // Test that value is being assigned
System.out.println("Age = " + age); // Test that value is being assigned
System.out.println("Fare = " + fare); // Test that value is being assigned
}
} finally {
if (s != null) {
s.close();
}
}
当我到达第一行的最后一列时,变量fare设置为第1行第6列和第2行第1列。由于某种原因,换行符不会触发while循环重新启动。
任何人都可以帮助我理解为什么while循环不会在行尾重启吗?
文件如下所示:
1 5 Bryan male 25 211.3375
1 2 Jimmy male 22 151.5500
大约有1200行。当运行此循环时,我尝试在第一行结束时设置fare = 211.3375时出现以下错误。由于某种原因,换行符不会重置循环。我只能假设换行符不被解释为制表符,但不知道如何纠正它。
Exception in thread "main" java.lang.NumberFormatException: For input string: "211.3375
1"