Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 shapes;

System.out.print("Enter a file name: ");

fileName = input.next();

shapes = create(fileName);

display(shapes);

input.close();

}

public static ArrayList create(String fileName) throws FileNotFoundException {

ArrayList shapes = new 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 shapes) {

for (int i = 0; i

System.out.println(shapes.get(i).toString() + " ");

}

}

}image text in transcribed

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.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

How much total revenue did that state generate overall?

Answered: 1 week ago