我正在使用Java(VSC是我的编译器)。 我尝试通读同一文件夹中的文档。但是,仅扫描会导致此错误:
An error has occured.
java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Project1.results(Project1.java:66)
at Project1.main(Project1.java:87)
这是我的完整代码:
public class Project1() {
public void results(String fileName){
double x, y, xc, yc, rad, radius;
int number_of_circles = 0;
try {
Scanner scanner = new Scanner(new BufferedReader(new FileReader(fileName)));
while(scanner.hasNext()) {
x = scanner.nextDouble();
y = scanner.nextDouble();
rad = scanner.nextDouble();
if(rad > 0) {
number_of_circles++;
}
}
}
catch(Exception exception) {
System.err.println("An error has occured.");
exception.printStackTrace();
}
}
public static void main(String args[]){
Project1 P = new Project1();
P.results("Project1.data");
}
}
我尝试了使用不同值的不同文件,但这似乎无济于事。谢谢。我查看了其他线程,但它们似乎并未涵盖完全相同的问题。 看起来如果我只在Project1.data中放入整数值,则可以正常工作,但显然我想允许其他值
Project1.data值:
9.50 2.40 3.20
2.20 3.40 5.60
2.50 2.40 3.20
3.20 4.40 5.60
答案 0 :(得分:2)
您的语言环境可能期望 $('#header > a').click(function(event){
event.preventDefault();
var id = $(this).attr('href');
$('html, body').animate({
scrollTop: $(id).offset().top;
}, 600);
})
作为分隔符,而文件中包含,
吗?
相关问题:Best way to parseDouble with comma as decimal separator?
这是另一个相关问题,提示.
:Java - Scanning comma delimited double and int values
答案 1 :(得分:0)
使用Scanner scanner = new Scanner(new File(fileName)).useLocale(Locale.US)
。下面给出的是经过测试的代码:
public void results(String fileName) {
double x, y, xc, yc, rad, radius;
int number_of_circles = 0;
try {
Scanner scanner = new Scanner(new File(fileName)).useLocale(Locale.US);
while (scanner.hasNext()) {
x = scanner.nextDouble();
y = scanner.nextDouble();
rad = scanner.nextDouble();
if (rad > 0) {
number_of_circles++;
}
System.out.println(x + " " + y + " " + rad);
}
} catch (Exception exception) {
System.err.println("An error has occured.");
exception.printStackTrace();
}
}
在Project1.data
中用以下内容进行了测试:
9.50 2.40 3.20
2.20 3.40 5.60
2.50 2.40 3.20
3.20 4.40 5.60
输出:
9.5 2.4 3.2
2.2 3.4 5.6
2.5 2.4 3.2
3.2 4.4 5.6