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.