Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help using Java my programs compile, however they crash when I run them.

ID: 3684840 • 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

class Car
{
   private String make;
   private String model;
   private int year;
    
   //constructor for the car class with scanner object as parameter
   public Car (String make, String model, int year)
   {
       this.make = make;
       this.model = model;
       this.year = year;
   }

   public String toString()
   {
       String result = year + " " + make + " " + model;
       return result;
   }

}

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class test

{

   public static void main(String [] args) throws IOException
   {
       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);
           BufferedReader fileReader = new BufferedReader(new FileReader(file));
           System.out.println();
        
           //Read file until the end
           while(true)
           {        
               String make;
               make = fileReader.readLine();
               if(make == null){ // reached at end of file
                   break;
               }
               //reader other params
               String model = fileReader.readLine();
               int year = Integer.parseInt(fileReader.readLine().trim());
             
               //Create a Car object using
               car [totalCars] = new Car(make, model, year);            
          
               //Print car summary
               System.out.println(car[totalCars]);

               totalCars++;
           }
           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.");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote