CSV 文本排序计算

【问题】

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 are 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

【回答】

JAVA硬编码处理csv很复杂,复用性也较差。可以用集算器作为文本处理的类库,脚本简单易懂。假设源文件以逗号为分隔符,排序时按第3列逆序(year)第4列逆序(runtime)第5列顺序(rating),则脚本如下

A
1=file("d:\\movies.txt").import(;,",")
2=A1.sort(_3:-1,_4:-1,_5)

 A1:读取文本

A2:按第三列降序,第四列降序,第五列升序排序

写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值