Question: In Java! Write a program that reads movie data from a csv ( comma separated values ) file and output the data in a formatted

In Java!
Write a program that reads movie data from a csv (comma separated values) file and output the data in a formatted table. The program first reads the name of the CSV file from the user. The program then reads the csv file and outputs the contents according to the following requirements:
Each row contains the title, rating, and all showtimes of a unique movie.
A space is placed before and after each vertical separator (|) in each row.
Column 1 displays the movie titles and is left justified with a minimum of 44 characters.
If the movie title has more than 44 characters, output the first 44 characters only.
Column 2 displays the movie ratings and is right justified with a minimum of 5 characters.
Column 3 displays all the showtimes of the same movie, separated by a space.
Each row of the csv file contains the showtime, title, and rating of a movie. Assume data of the same movie are grouped in consecutive rows.
Ex: If the input of the program is:
movies.csv
This is the coding I got so far but it's giving me "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at LabProgram.main(LabProgram.java:21)":
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
public class LabProgram {
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter the name of the CSV file: ");
String fileName = scnr.nextLine();
try (Scanner fileScanner = new Scanner(new FileInputStream(fileName))){
String previousTitle ="";
boolean firstShowtime = true;
while (fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
String[] parts = line.split(",");
String showtime = parts[0];
String title = parts[1];
String rating = parts[2];
// Process each row
if (!title.equals(previousTitle)){
if (!firstShowtime){
System.out.println(); // Move to the next line after the first showtime
}
displayRow(title, rating, showtime);
firstShowtime = false;
} else {
System.out.print(""+ showtime); // Add showtime to the current row
}
previousTitle = title;
}
}
}
private static void displayRow(String title, String rating, String showtime){
// Display the row with proper formatting
System.out.printf("%-44s |%5s|", title.length()>44? title.substring(0,44) : title, rating);
System.out.print(""+ showtime);
}
}
 In Java! Write a program that reads movie data from a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!