[英]Getting a InputMismatch error and I don't know why
当我尝试运行该程序时,我目前收到此错误。 代码编译得很好,但是当我尝试运行程序时,它给了我这个异常
Exception in thread "main" 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 Runner.main(Runner.java:23)
我尝试注释掉分隔符,但它给了我这个错误
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Box.getSurfaceArea()" because "<parameter1>[<local5>]" is null
at Runner.averageSurfaceArea(Runner.java:33)
at Runner.main(Runner.java:25)
老实说,我不明白为什么会出现这些错误,也不知道该怎么做。 这是跑步者 class 和 object 类/
//runner
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
class Runner{
public static void main(String [] args) throws IOException{
File f = new File ("input.txt");
Scanner scan = new Scanner(f);
int i = 0;
int count = 0;
int index = 0;
while(scan.hasNextLine()){//getting the array size
scan.nextLine();
count++;
}
Scanner scan2 = new Scanner(f);
scan2.useDelimiter(" ");
Box [] boxarr = new Box [count]; // Box array
while(scan2.hasNextDouble()){
boxarr[i] = new Box();
boxarr[i].setLength(scan2.nextDouble());//setting atrubutes
boxarr[i].setHeight(scan2.nextDouble());
boxarr[i].setWidth(scan2.nextDouble());
}
averageSurfaceArea(boxarr);//uses this method to get average surface area
index = getBigBox(boxarr); // gets the largest box
}
public static double averageSurfaceArea(Box [] arr){
double average = 0;
double surfaceAreaTotal = 0;
for(int i = 0; i<arr.length;i++){
surfaceAreaTotal = surfaceAreaTotal + arr[i].getSurfaceArea();
}
average = surfaceAreaTotal / arr.length;
return average;
}
public static int getBigBox(Box [] arr){
int index = 0;
for(int i = 0 ; i<arr.length-1; i++){
if(arr[i].compareTo(arr[i+1])){
index = i;
}else{
index = i+1;
}
}
return index;
}
}
//object class
public class Box{
private double length;
private double height;
private double width;
Box(){}
//constructures
Box(double length, double height, double width){
this.length = length;
this.height = height;
this.width = width;
}
//setters
public void setLength(Double length){
this.length = length;
}
public void setHeight(Double height){
this.height = height;
}
public void setWidth(Double width){
this.width = width;
}
//getters
public double getLength(){
return this.length;
}
public double getHeight(){
return this.height;
}
public double getWidth(){
return this.width;
}
//Gets the surface area
public double getSurfaceArea(){
return 2*(this.length*this.width + this.length*this.height + this.width*this.height);
}
//compares the box objects
public boolean compareTo(Box box){
return 2*(this.length*this.width + this.length*this.height + this.width*this.height) >= box.getSurfaceArea();
}
}
这是输入文件。 它被命名为 input.txt
8.2 17.1 2.9
3.4 4.6 3.1
1.4 19.9 1.6
4.9 6.7 5.3
18.5 2 16.3
4.0 7.2 10.8
任何帮助表示赞赏!
double 中的分隔符在您的系统上可能不同,eq "," 您可以通过更改 "." 来检查它是否有效。 到 "," 或者只是在扫描仪上设置区域设置:
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
在您的跑步者 class 中,删除您的第 20 行,即 scan2.useDelimiter(" "); 并且您的代码应该可以正常运行。在 java 中,delimiter 方法默认在扫描仪中搜索空格,因此您不需要使用 delimiter 方法。
除此之外,您不会增加变量“i”。在您的 while 循环中,在第 26 行之后包含 i++。 此外,在您的第 10 行 File f = new File ("input.txt"); 写入输入文件的完整路径,而不是写入文件名。 修复这三件事,您的代码将为您提供所需的 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.