I need help with the Java code so that it can output as shown in the image below: Input is a csv file like this - (The csv file is : 16:40,Wonders of the World,G 20:00,Wonders of the World,G 19:00,Journey to Space ,PG-13 12:45,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG 15:00,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG 19:30,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG 10:00,Adventures of Lewis and Clark,PG-13 14:30,Adventures of Lewis and Clark,PG-13 19:00,Halloween,R ) The code the I need help fixing: 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); // Read the file name from the user String fileName = scnr.nextLine(); // Open the CSV file try (Scanner fileScanner = new Scanner(new FileInputStream(fileName))) { while (fileScanner.hasNextLine()) { // Read each line from the CSV file String line = fileScanner.nextLine(); // Split the line into showtime, title, and rating String[] movieData = line.split(","); // Extract showtime, title, and rating String showtime = movieData[0]; String title = movieData[1].length() > 44 ? movieData[1].substring(0, 44) : movieData[1]; String rating = movieData[2]; // Print the formatted output System.out.printf("%-44s | %5s | %s%n", title, rating, showtime); } } } }
I need help with the Java code so that it can output as shown in the image below:
Input is a csv file like this
-
(The csv file is :
16:40,Wonders of the World,G
20:00,Wonders of the World,G
19:00,Journey to Space ,PG-13
12:45,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
15:00,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
19:30,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
10:00,Adventures of Lewis and Clark,PG-13
14:30,Adventures of Lewis and Clark,PG-13
19:00,Halloween,R
)
The code the I need help fixing:
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);
// Read the file name from the user
String fileName = scnr.nextLine();
// Open the CSV file
try (Scanner fileScanner = new Scanner(new FileInputStream(fileName))) {
while (fileScanner.hasNextLine()) {
// Read each line from the CSV file
String line = fileScanner.nextLine();
// Split the line into showtime, title, and rating
String[] movieData = line.split(",");
// Extract showtime, title, and rating
String showtime = movieData[0];
String title = movieData[1].length() > 44 ? movieData[1].substring(0, 44) : movieData[1];
String rating = movieData[2];
// Print the formatted output
System.out.printf("%-44s | %5s | %s%n", title, rating, showtime);
}
}
}
}


Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









