Results 1 to 4 of 4
  1. #1
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Movie rating ystem

    In this assignment I have to make a movie rating program that reads from a text file
    Text.txt
    Java Code:
    7
    Harry Potter and the Half-Blood Prince
    4
    Harry Potter and the Half-Blood Prince
    5
    Army of the Dead
    1
    Harry Potter and the Half-Blood Prince
    4
    Army of the Dead
    2
    The Uninvited
    4
    Pandorium
    The program then takes the the movie name and the rating above it and puts it in a Hashmap and prints them out.
    What I need the program to do is average the ratings of the occurances that happen more than once and print it out. So far this is all I have
    Java Code:
    public class Rating {
        public static void main(String[] args) throws IOException {
            Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Dmp\\Desktop\\DPeraltaPS1\\Text.txt"));
            String line = null;
            while ((line = br.readLine()) != null) {
                //Rating
                int rating = Integer.parseInt(line);
    
                //Movie name
                line = br.readLine();
    
                List<String> movieList = map.get(rating);
    
                if(movieList == null) {
                    movieList = new ArrayList<String>();
                    map.put(rating, movieList);
                }
    
    
    
                    //Adding movie name to list
                movieList.add(line);
                System.out.println(line+" "+rating);
    
            }
        }
    }
    Which outputs me
    Java Code:
    Harry Potter and the Half-Blood Prince 7
    Harry Potter and the Half-Blood Prince 4
    Army of the Dead 5
    Harry Potter and the Half-Blood Prince 1
    Army of the Dead 4
    The Uninvited 2
    Pandorium 4
    How can I get it to make the movies with the same name into one and average the values

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Movie rating ystem

    Use a Map with the key being the name and the value being a list of the ratings. After all the data has been read, the values in the list can be averaged.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Movie rating ystem

    I made the map but how do I get the map to read the text file how it is currently formatted

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Movie rating ystem

    how do I get the map to read the text file how it is currently formatted
    Sorry, that does not make sense. Maps do not read files. The Scanner class has methods that can be used to read simple text files.
    What is wrong with the code you are currently using?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. movie
    By okabeer in forum AWT / Swing
    Replies: 3
    Last Post: 11-17-2011, 12:25 AM
  2. Movie
    By johanl in forum Advanced Java
    Replies: 0
    Last Post: 03-08-2011, 07:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •