Im trying to write a JAVA program that will allow me to input movies.csv and ouput "Wonders of the World | G | 16:40 20:00 End of the Universe | NC-17 | 19:00 Buffalo Bill And The Indians or Sitting Bull | PG | 12:45 15:00 19:30 Adventure of Lewis and Clark | PG-13 | 10:00 14:30 Halloween | R | 19:00". I need help with my code, this is what it looks like now, please let me know how to fix it. import java.io.File; import java.io.FileNotFoundException; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Scanner; public class LabProgram { public static void main(String[] args) { // Create LinkedList LinkedList movies = new LinkedList<>(); // Create HashSet LinkedHashSet moviesTitle = new LinkedHashSet<>(); // read file name from user Scanner input = new Scanner(System.in); String fileName = input.next(); try { // create an object of Scanner Scanner file = new Scanner(new File("movies.csv")); // iterate each line in the file while (file.hasNextLine()) { String data = file.nextLine(); movies.add(data); String[] dataArray = data.split(",");// moviesTitle.add(dataArray[1]); } } catch (FileNotFoundException e) { e.printStackTrace(); } // format each line for (String title : moviesTitle) { // print title System.out.printf("%s", title); String time = " | "; String ratings = " | "; for (String movie : movies) { String[] dataArray = movie.split(","); if (title.equals(dataArray[1])) { time = time + dataArray[0] + " "; ratings = ratings + dataArray[2] + ","; } } // print ratings System.out.printf("%s", ratings.substring(0, ratings.length() - 1)); // print times System.out.printf("%s", time.substring(0, time.length() - 1)); // print vertical bar System.out.println(); } } } Thr first image attatched shows what its supposed to look like and the 2nd image shows what it looks like when I try to run
Im trying to write a JAVA program that will allow me to input movies.csv and ouput "Wonders of the World | G | 16:40 20:00 End of the Universe | NC-17 | 19:00 Buffalo Bill And The Indians or Sitting Bull | PG | 12:45 15:00 19:30 Adventure of Lewis and Clark | PG-13 | 10:00 14:30 Halloween | R | 19:00".
I need help with my code, this is what it looks like now, please let me know how to fix it.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
// Create LinkedList
LinkedList<String> movies = new LinkedList<>();
// Create HashSet
LinkedHashSet<String> moviesTitle = new LinkedHashSet<>();
// read file name from user
Scanner input = new Scanner(System.in);
String fileName = input.next();
try {
// create an object of Scanner
Scanner file = new Scanner(new File("movies.csv"));
// iterate each line in the file
while (file.hasNextLine()) {
String data = file.nextLine();
movies.add(data);
String[] dataArray = data.split(",");//
moviesTitle.add(dataArray[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// format each line
for (String title : moviesTitle) {
// print title
System.out.printf("%s", title);
String time = " | ";
String ratings = " | ";
for (String movie : movies) {
String[] dataArray = movie.split(",");
if (title.equals(dataArray[1])) {
time = time + dataArray[0] + " ";
ratings = ratings + dataArray[2] + ",";
}
}
// print ratings
System.out.printf("%s", ratings.substring(0, ratings.length() - 1));
// print times
System.out.printf("%s", time.substring(0, time.length() - 1));
// print vertical bar
System.out.println();
}
}
}
Thr first image attatched shows what its supposed to look like and the 2nd image shows what it looks like when I try to run
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 5 images