I am currently getting this error when I try to run the program. The code compiles fine but when I try to run the program, it gives me this exeception
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)
I tried commenting out the delimiter but it gives me this error
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)
Honestly I do not understand why I am getting these errors and I do not know what to do. Here is the runner class and object class/
//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();
}
}
This is the input file. It is named 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
Any help is appreciated!
CodePudding user response:
Delimiter in double can be different on your system, e.q. "," You can check if it works by changing "." to "," or just set locale on Scanner:
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
CodePudding user response:
In your runner class, delete your 20th line which is scan2.useDelimiter(" "); and your code should run just fine.In java, delimiter method by default searches for whitespace in scanner so you don't need to use the delimiter method.