对从文本文件

时间:2015-10-05 04:56:20

标签: java arrays sorting

我对Java和编程很新(比如一个月的经验),我有这个任务。程序必须从文本文件中提取信息并以各种方式对其进行排序。文本文件中的数据始终以给定顺序以CSV格式存储:标题,绘图,年份,运行时,评级,演员,导演。总会有3行文字。我已经把数据拉到数组中了,但我仍然坚持如何对数据进行排序。需要排序的方式是最短/最长的运行时间,最旧/最新的电影,以及按评级显示。我已经尝试过转换为int数组和arrayList但我不确定如何实现任何东西。如果有人能帮助我,那将是非常棒的。基本上我只是对如何对数据进行排序感到困惑。这是我到目前为止所做的:

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

1 个答案:

答案 0 :(得分:1)

确定。首先,您需要创建一些类,比如Movie类,以提供一些排序上下文。字符串可以是任何字符串,它只是一个字符序列。但想想一个Movie对象,就像这样:

public class Movie {
    String title;
    String plot;
    String year;
    String runtime;
    double rating;

    List<String> actors;
    String director;
// Setters and getters

   public String toString(){
       return "Rating : " + getRating();
    }
}

现在不需要对String数组进行排序,而是需要对Movie对象的数组(或集合)进行排序。我将向您展示如何根据评级(从最低到最高)对电影列表进行排序。

首先,研究“比较器”&#39;对象。 http://www.programcreek.com/2011/12/examples-to-demonstrate-comparable-vs-comparator-in-java/

    public class MovieRatingComparator implements Comparator<Movie>{

    @Override
    public int compare(Movie o1, Movie o2) {
        //If o1's rating > o2's rating , return 1 , if it is less than o2's rating ,return -1 , else return 0.
        return o1.getRating() > o2.getRating() ? 1 : o1.getRating() < o2.getRating() ? -1 : 0;
    }

}

现在,不是尝试对String数组进行排序,而是获取List<Movie>对象或电影数组,并将其传递给它,如下所示:

public class Test {
    public static void main(String[] args) {
        Movie m1 = new Movie();
        m1.setRating(1.2);

        Movie m2= new Movie();
        m2.setRating(2.3);

        Movie m3= new Movie();
        m3.setRating(9);

        List<Movie> list = Arrays.asList(m1,m2,m3);
        Collections.sort(list,new MovieRatingComparator());
        System.out.println(list);
    }
}

您的输出将是:

[Rating : 1.2, Rating : 2.3, Rating : 9.0]