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

AMERICAN SNIPER,2014,11111,10,STANDARD THE GUARDIAN,2006,11112,10,STANDARD THE S

ID: 641790 • Letter: A

Question

AMERICAN SNIPER,2014,11111,10,STANDARD
THE GUARDIAN,2006,11112,10,STANDARD
THE SANTA CLAUSE,1994,11113,10,STANDARD
SPIDERMAN,2002,11114,5,BLURAY
HERCULES,1992,11115,5,BLURAY
PLANET OF THE APES,2014,11116,5,BLURAY
POKEMON,1995,11222,8,WII
EVOLVE,2015,12223,8,XBOX
SAINTS ROW,2006,12323,8,PS

"Read the items from the text file(use tokenizer), creates the approapriate class instance of each item and stores them in an arraylist whose data type is renatl item."

This is my code so far, do not know how to add it to the arraylist

public static void main(String[] args) {

       ArrayList<RentalItem> ri = new ArrayList<RentalItem>();

       String filename;
       String data;
       Scanner kb = new Scanner(System.in);

       System.out.print("Enter File Name: ");
       filename = kb.nextLine();

       try {
           File inFile = new File(filename);
           Scanner inputFile = new Scanner(inFile);
           while (inputFile.hasNext()) {
               data = inputFile.nextLine();
               System.out.println(data);

               StringTokenizer tokens = new StringTokenizer(data, ";");
               String title = tokens.nextToken();
               int year = Integer.parseInt(tokens.nextToken());
               int upc = Integer.parseInt(tokens.nextToken());
               int stock = Integer.parseInt(tokens.nextToken());
               String platform = tokens.nextToken();
               String movietype = tokens.nextToken();
           }
          
          
           inputFile.close();
       } catch (FileNotFoundException fileError) {
           System.out.println(filename + " cannot be found!");

       } catch (NoSuchElementException dataError) {
           System.out.println("Error reading file!");

       }

   }

}

Explanation / Answer

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

class RentalItem{
   private int year;
   private int upc;
   private int stock;
   private String platform;
   private String title;
  
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }
   public int getUpc() {
       return upc;
   }
   public void setUpc(int upc) {
       this.upc = upc;
   }
   public int getStock() {
       return stock;
   }
   public void setStock(int stock) {
       this.stock = stock;
   }
   public String getPlatform() {
       return platform;
   }
   public void setPlatform(String platform) {
       this.platform = platform;
   }
  
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
  
   @Override
   public String toString() {
      
       return this.title+","+this.year+","+this.upc+","+this.stock+this.platform;
   }
  
}

public class Client {

   public static void main(String[] args) {

           ArrayList<RentalItem> ri = new ArrayList<RentalItem>();

           String filename;
           String data;
           Scanner kb = new Scanner(System.in);

           System.out.print("Enter File Name: ");
           filename = kb.nextLine();

           try {
               File inFile = new File(filename);
               Scanner inputFile = new Scanner(inFile);
               while (inputFile.hasNext()) {
                   data = inputFile.nextLine();
                   //System.out.println(data);
                   RentalItem rentalItem=new RentalItem();// creating the object of RentalItem
                   StringTokenizer tokens = new StringTokenizer(data, ",");
                   String title = tokens.nextToken();
                   int year = Integer.parseInt(tokens.nextToken());
                   int upc = Integer.parseInt(tokens.nextToken());
                   int stock = Integer.parseInt(tokens.nextToken());
                   String platform = tokens.nextToken();
                   rentalItem.setPlatform(platform);
                   rentalItem.setTitle(title);
                   rentalItem.setUpc(upc);
                   rentalItem.setYear(year);
                   rentalItem.setStock(stock);
                   ri.add(rentalItem);//adding the object in arraylist
               
               
               }
              System.out.println("Data in arraylist: "+ri);
            
               inputFile.close();
           } catch (FileNotFoundException fileError) {
               System.out.println(filename + " cannot be found!");

           } catch (NoSuchElementException dataError) {
               System.out.println("Error reading file!");

           }

       }

   }

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