I need help using Java my programs compile, however they crash when I run them.
ID: 3684944 • Letter: I
Question
I need help using Java my programs compile, however they crash when I run them. I am trying to build an array of Car objects (no more than 100) from a file, upon reaching the end of the file I need to output the attributes of each Car (Make, Model, Year) using println.
class Car
{
private String make;
private String model;
private int year;
//constructor for the car class with scanner object as parameter
public Car (Scanner fileScanner)
{
make = fileScanner.nextLine();
model = fileScanner.nextLine();
year = fileScanner.nextInt();
}
public String toString()
{
String result = year + " " + make + " " + model;
return result;
}
}
CarsFromFile
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class test
{
public static void main(String [] args)
{
System.out.println("This is a test program.");
//Get filename from the user
System.out.print("Please enter file name: ");
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine();
Car [] car = new Car[100];
int totalCars = 0;
try
{
File file = new File(fileName);
Scanner fileScanner = new Scanner(file);
System.out.println();
//Read file until the end
for(int i=0; fileScanner.hasNext(); i++)
{
//Create a Car object using
car [i] = new Car(fileScanner);
//Print car summary
System.out.println(car);
totalCars +=1;
}
System.out.println("Total cars in the file are: " + totalCars);
}
catch (FileNotFoundException ex)
{
System.out.println(" File " + fileName + " was not found.");
}
System.out.println();
System.out.println("Program complete.");
}
}
CarFile
Fiat
500
2013
Jeep
Wrangler
2004
Ford
Fusion
2012
Explanation / Answer
I have modified the program, please use the below given program:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
class Car
{
private String make;
private String model;
private String year;
//constructor for the car class with scanner object as parameter
public Car (Scanner fileScanner)
{
this.make = fileScanner.nextLine();
this.model = fileScanner.nextLine();
this.year = fileScanner.nextLine();
}
@Override
public String toString()
{
String result = year + " " + make + " " + model;
return result;
}
}
public class test
{
public static void main(String [] args)
{
System.out.println("This is a test program.");
//Get filename from the user
System.out.print("Please enter file name: ");
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine();
Car [] car = new Car[100];
int totalCars = 0;
try
{
File file = new File(fileName);
Scanner fileScanner = new Scanner(file);
System.out.println();
//Read file until the end
for(int i=0;i<100; i++)
{
//Create a Car object using
car [i] = new Car(fileScanner);
//Print car summary
System.out.println(car[i]);
totalCars +=1;
}
System.out.println("Total cars in the file are: " + totalCars);
}
catch (FileNotFoundException ex)
{
System.out.println(" File " + fileName + " was not found.");
}
System.out.println();
System.out.println("Program complete.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.