0

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

1 Answer 1

1

Ok. Firstly, you would need to create some class , say Movie class ,to provide some context for sorting. A string can be anything, it is just a sequence of characters. But think of a Movie object,like so :

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();
    }
}

Now instead of sorting a String array, you need to sort an array (or a collection) of Movie objects. I will show you how to sort list of movies on the bases of ratings (lowest to highest).

First,study up on 'Comparator' objects. 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;
    }

}

Now, instead of trying to sort a String array, obtain a List<Movie> object or an array of movies, and pass it sort method like so:

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);
    }
}

Your output would be :

[Rating : 1.2, Rating : 2.3, Rating : 9.0]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for taking the time to type this up. This kind of clears some things up. I'm still a little confused on how to create and populate the comparators based on what's in the text file. The program should work with any text file that's plugged into it, as long as it's formatted right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.