Search code examples
javaarraylistbufferedreader

Searching and sorting through an Arraylist


I am a new Java programmer and I am working on a project that requires me to read a text file that has Movie Reviews.

Once I've read the file, I am asked to search and sort through the Array of movies and return the total number of reviews for each movie as well as the average rate for each movie.

The portion I am currently stuck on is iterating through the Array list.

I am using an inner and outer for loop and I seem to be getting an infinite loop.

I would appreciate a second set of eyes. I have been staring at this project for a few days now and starting to not see mistakes.

Here is the code:

import java.io.*;
import java.util.*;
import java.lang.*;

public class MovieReviewApp {

public static void main(String[] args)
{

    String strline = "";
    String[] result = null;
    final String delimit = "\\s+\\|\\s+";
    String title ="";
    //int rating = (Integer.valueOf(- 1));

    ArrayList<MovieReview> movies = new ArrayList<MovieReview>();
    //ArrayList<String> titles = new ArrayList<String>();
    //ArrayList<Integer> ratings = new ArrayList<Integer>();
    //HashMap<String, Integer> hm = new HashMap<String, Integer>();
    //ListMultimap<String, Integer> hm = ArrayListMultimap.create();

   try
   {

      BufferedReader f = new BufferedReader(new FileReader("/Users/deborahjaffe/Desktop/Java/midterm/movieReviewData.txt"));

        while(true)
        {
            strline = f.readLine(); // reads line by line of text file

            if(strline == null)
            {
                break;
            }

            result = strline.split(delimit, 2); //creates two strings

            //hm.put(result[0], new Integer [] {Integer.valueOf(result[1])});
            //hm.put(result[0], Integer.valueOf(result[1]));

            // titles.add(result[0]);
            //ratings.add(Integer.valueOf(result[1]));

            MovieReview m = new MovieReview(result[0]);
            movies.add(m);
            MovieReview m2 = new MovieReview();

            int rating = Integer.valueOf(result[1]);
            int sz = movies.size();


            for (int i = 0; i < sz; i++)
            {
                for (int j = 0; j < sz; j++)
                {
                    m2 = movies.get(i);

                   if (movies.contains(m2))
                   {
                       m2.addRating(rating);
                   }

                   else
                   {
                       movies.add(m2);
                       m2.addRating(rating);
                   }
               }
            }

            movies.toString();

            //Collections.sort(movies);

       } //end while

       f.close();

       //Set<String> keys = hm.keySet();
       //Collection<Integer> values = hm.values();

    } //end of try

    catch(FileNotFoundException e)
    {
        System.out.println("Error: File not found");
    }
    catch(IOException e)
    {
        System.out.println("Error opening a file.");
    }

} // end main

} // end class

Solution

  • Read the file first and then iterate through the list or map for searching, sorting etc. In the above code, close the while loop before iterating through the list.