0

For this program I'm supposed to read from a txt file containing information on the automobile's make, model, mpg, and trunk space in that order. An example is: hyundai genesis 24.6 100 and repeated for several different cars. We had to construct an "Automobile" superclass with the instance variables of make and model. Then a "GasEngineAuto" subclass with instance variable for mpg that extends "Automobile". Then a subclass called "Sedan" that extends "GasEngine Auto" and has instance variable for trunk space. For the assignment we had to get these classes signed off on to make sure that they made correctly.

Write a method to read the information from the file gas_sedans.txt and store it in the list you created in the previous step. The list should be the only parameter for this method. Within the method, open the file, read the information for each sedan into appropriately-typed variables, call the parameterized constructor (the one that takes arguments for all attributes) to create the object, then add the object to the list. Call this method from main.

Below is my code so far. I wanted to try to make sure I could read into the arrayList before I used a method to do it.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author 
 */
public class lab10_prelab {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        
        ArrayList<Sedan> sedanList = new ArrayList<Sedan>();

        File myFile = new File(System.getProperty("user.dir")+"/src/gas_sedans (1).txt");
        if (!myFile.exists()){
            System.out.println("The file could not be found");
            System.exit(0);
        }

        Scanner inputFile = new Scanner(myFile);
        
        while (inputFile.hasNext())
        {
            Sedan a = new Sedan();
            a.setMake(inputFile.nextLine());
            a.setModel(inputFile.nextLine());
            inputFile.nextLine();
            a.setMPG(inputFile.nextDouble());
            inputFile.nextLine();
            a.setTrunkCapacity(inputFile.nextDouble());
            sedanList.add(a);
        }
        inputFile.close();
        }
        }

It says that I get a InputMismatchException notice when I try to run the program.

2 Answers 2

1

You have typo error: automobileArray and autombileArray are different.

In your code you are missing o after m in the variable

autombileArray
    ^

Edited: 06:49am 18/07/20115

It says that I get a InputMismatchException notice when I try to run the program.

a.setModel(inputFile.nextLine());
inputFile.nextLine();//remove this line
a.setMPG(inputFile.nextDouble());
inputFile.nextLine();
a.setTrunkCapacity(inputFile.nextDouble());
inputFile.nextLine();//add here
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I corrected that and made a method. Now I need to make another to output the arrayList using the toString method.
0

I made some changes and arrived at this. Now it says to: Write a method to display the list. The method should have one parameter, an ArrayList of Automobiles. Within the body of the method, call upon the toString method to display each item. Call this method at the end of main. Does this mean make a class like "public String toString()... or is what I did fine.

public class Smith_lab10_prelab {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {

        ArrayList<Automobile> AutomobileList = new ArrayList<Automobile>();
        fillAutomobileList(AutomobileList);
        displayAutomobileList(AutomobileList);
    }

    public static void fillAutomobileList(ArrayList sedanList) throws FileNotFoundException {
        File myFile = new File(System.getProperty("user.dir") + "/src/gas_sedans (1).txt");
        if (!myFile.exists()) {
            System.out.println("The file could not be found");
            System.exit(0);
        }

        Scanner inputFile = new Scanner(myFile);
        for (int i = 0; inputFile.hasNext(); i++) {
            Sedan a = new Sedan();
            a.setMake(inputFile.next());
            a.setModel(inputFile.next());
            a.setMPG(inputFile.nextDouble());
            a.setTrunkCapacity(inputFile.nextDouble());
            sedanList.add(a);
        }
    }

    public static void displayAutomobileList(ArrayList automobileList) {
        System.out.println(automobileList.toString());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.