从文件读取双精度时输入不匹配 [英] Input mismatch when reading double from file
问题描述
我必须编写一个程序,它将从文本文件balances.txt中读取名称和余额,并组织成一个报告,然后将余额汇总为总计。这是文件包含的内容:
I have to write a program that will read the names and balances from text file "balances.txt" and organize into a report that will then sum up the balances into a total. This is what the file contains:
JAKIE JOHNSON,2051.59
SAMUEL PAUL SMITH,10842.23
ELISE ELLISON,720.54
我原本写的代码给了我我想要的东西,但被告知不使用循环,数组或 parseDouble
。我现在尝试了以下操作,但每次使用 nextDouble
时,我都会收到错误。代码:
I had originally written the code which gave me exactly what I wanted, but was told not use loops, arrays, or parseDouble
. I've now tried the following, but I keep getting an error every time I used nextDouble
. The code:
import java.io.File;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.util.Scanner ;
public class BankFile {
public static void main(String[] args) throws IOException {
Scanner fileIn = null;
try {
String filename = "balances.txt" ;
File newFile = new File(filename);
Scanner in = new Scanner(newFile);
in.useDelimiter(",");
String name = in.next();
System.out.println(name);
// trying to see if first name will display
double money = in.nextDouble();
System.out.println(money);
// trying to see if first double will display
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
}
}
}
这是输出和异常stacktrace:
This is the output and exception stacktrace:
JAKIE JOHNSON
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at Lab2.main(BankFile.java:52) `
推荐答案
如果你看看 Javadoc :
useDelimiter
useDelimiter
public Scanner useDelimiter(String pattern)
将此扫描仪的分隔模式设置为由指定字符串构造的模式。
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
现在,如果你看看在怎么样你做你的:
Now if you take a look at how you do yours:
in.useDelimiter(",");
这将使用逗号作为分隔符,现在让我们来看看你的文本文件:
This will use commas as the delimiter, now let's take a look at your text file:
JAKIE JOHNSON,2051.59
SAMUEL PAUL SMITH,10842.23
ELISE ELLISON,720.54
起初看起来似乎逗号很好,但是既然已经设置了分隔符,就会发生这种情况:
At first it may seem that commas are fine, but since you've set the delimiter, this is what happens:
首先你打电话给 in.next()
返回:
JAKIE JOHNSON,2051.59
^^^^^^^^^^^^^
没关系,但是当你打电话给 in.nextDouble()
时,会发生以下情况:
That's fine, but when you then call in.nextDouble()
, the below happens:
JAKIE JOHNSON,2051.59
^^^^^^^
SAMUEL PAUL SMITH,10842.23
^^^^^^^^^^^^^^^^^
如您所见,下一行也会被选中双,这不是一个有效的双。这会导致Java报告 InputMismatchException
,因为预期的输入不是你得到的 - 一个字符串。要解决此问题,请使用正则表达式来分隔换行符:
As you can see, the next line is also selected along with the double, which isn't a valid double. This causes Java to report an InputMismatchException
, as the expected input isn't what you get - a string. To combat this, use a regular expression to also delimit newlines:
in.useDelimiter(",|\n");
这将匹配新行和逗号,因此它将正确分隔。管道( |
)表示它将界定 。这将正确输出:
This will match new-lines and commas, so it will delimit correctly. The pipe (|
) means that it will delimit either. This will correctly output:
JAKIE JOHNSON
2051.59
这篇关于从文件读取双精度时输入不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!