Question
My code keeps throwing a java.util.InputMismatchException and my .txt has the right data type. My code is below and a picture of my file. package
My code keeps throwing a java.util.InputMismatchException and my .txt has the right data type.
My code is below and a picture of my file.
package shape;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class Helper {
public static void start() throws IOException {
Scanner input = new Scanner(System.in);
String fileName;
ArrayList
System.out.print("Enter a file name: ");
fileName = input.next();
shapes = create(fileName);
display(shapes);
input.close();
}
public static ArrayList
ArrayList
File file = new File(fileName);
Scanner inputShapeFile = new Scanner(file);
String shapeType, shapeName;
double length, width, radius, sideOne, sideTwo, sideThree;
if (!(file.exists())) {
System.out.println("The file " + file + " does not exist.");
System.exit(0);
}
while (inputShapeFile.hasNextLine()) {
shapeType = inputShapeFile.nextLine();
inputShapeFile.nextLine();
switch(shapeType) {
case "Rectangle":
Rectangle rectangle = new Rectangle();
shapeName = inputShapeFile.nextLine();
length = inputShapeFile.nextDouble();
width = inputShapeFile.nextDouble();
inputShapeFile.next();
rectangle.setName(shapeName);
rectangle.setLength(length);
rectangle.setWidth(width);
shapes.add(rectangle);
break;
case "Circle":
Circle circle = new Circle();
shapeName = inputShapeFile.nextLine();
radius = inputShapeFile.nextDouble();
inputShapeFile.nextDouble();
circle.setName(shapeName);
circle.setRadius(radius);
shapes.add(circle);
break;
case "Triangle":
Triangle triangle = new Triangle();
shapeName = inputShapeFile.nextLine();
sideOne = inputShapeFile.nextDouble();
sideTwo = inputShapeFile.nextDouble();
sideThree = inputShapeFile.nextDouble();
triangle.setName(shapeName);
triangle.setSideOne(sideOne);
triangle.setSideTwo(sideTwo);
triangle.setSideThree(sideThree);
shapes.add(triangle);
break;
default:
break;
}
}
inputShapeFile.close();
return shapes;
}
public static void display(ArrayList
for (int i = 0; i
System.out.println(shapes.get(i).toString() + " ");
}
}
}
shape.txt- Notepad File Edit Format View Help Rectangle square 12.8 12. 8 circle Round 4. 50 Triangle Pythag 4.0 6.0 8.0Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started