CSV File Sorting
【Question】
I'm quite new to Java and programming in general (like a month of experience) and I have this assignment. The program must pull info from a text file and sort it in various ways. The data in the text file is always stored in CSV format in the given order: Title,Plot,Year,Runtime,Rating,Actors,Director. There will always be 3 lines of text. I've gotten as far as pulling the data and separating it into arrays, but I'm stuck on how to sort it. The way it needs to be sorted is shortest/longest runtime, oldest/newest movie, and displaying by rating. I've tried converting to int arrays and arrayList but I'm not sure how to implement anything. If anyone could help me out that would be awesome. Basically I'm just confused on how to sort the data. Here's what I have so far:
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class movieData{
public static void main(String[] args){
String fileName = "./movies.txt";
String[] lines = new String[3];
readFile(lines, fileName);
} //end main
//method used for populating a String array with data from a file.
//input: String[], String
//output: none #Array has been modified
public static void readFile(String[] lines, String fileName){
int counter = 0;
try{
Scanner fromFile = new Scanner(new File(fileName));
while(fromFile.hasNextLine()){
lines[counter] = fromFile.nextLine();
counter++;
}//end while
}//end try block
catch(FileNotFoundException e){
System.out.println("File not found.");
}//end catch block
displayMenu(lines);
}//end readFile
public static void displayMenu(String[] lines){
Scanner kb = new Scanner(System.in);
int choice;
while(true){
System.out.println("\n1: Display all movies");
System.out.println("2: Display shortest movie");
System.out.println("3: Display longest movie");
System.out.println("4: Display oldest movie");
System.out.println("5: Display newest movie");
System.out.println("6: Display movies by rating");
System.out.println("0: Quit the program");
System.out.print("Choice:");
choice = kb.nextInt();
if(choice == 0)
break;
options(choice, lines);
}//end while
}//end displayMenu
public static void options(int choice, String[] lines){
if(choice == 1){
System.out.println("Movie 1:"+lines[0]);
System.out.println("Movie 2:"+lines[1]);
System.out.println("Movie 3:"+lines[2]);
}//end if
if(choice == 2){
oldestMovie(lines);
}//end if
}//end options
public static void oldestMovie(String[] lines){
}//end oldestMovie
}//end class
The text file that I'm supposed to use is populated like this:
The Matrix Machines enslave humans with virtual reality 1999 136 R Keanu Reeves and Laurence Fishburne Wachowski Brothers
Pulp Fiction Two thugs boxer and crime boss meet their fates 1994 154 R John Travolta and Samuel L. Jackson Quentin Tarantino
Blade Runner Futuristic detective hunts obsolete androids 1982 122 R Harrison Ford and Sean Young Ridley Scott
【Answer】
It’s complicated to sort a csv file with Java hardcode and the code is unusable. esProc can be used as a file processing class library to handle this type of scenarios. An SPL script is concise and easy to understand. To sort your csv file by column 3 (year) and column 4 (runtime) in descending order and by column 5 (rating) in ascending order, the script looks like this:
A |
|
1 |
=file("d:\\movies.txt").import(;,",") |
2 |
=A1.sort(_3:-1,_4:-1,_5) |
A1: Read the csv file.
A2: Sort the file by column 3 (year) and column 4 (runtime) in descending order and by column 5 (rating) in ascending order
See How to Call an SPL Script in Java to learn how to call an SPL script in a Java application.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL