user3320634
user3320634

Reputation: 53

putting text file data into a hash map

my data in the text file looks like this...

3
movie title
4
movie title
1
movie title

the number on top is the movie rating and the text under it is the movie title.

The code I have so far is below. But It's not printing anything out except empty brackets! Sample code would be appreciated!

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MovieReview {

    public static void main(String[] args) {
            Map<Integer, String> map = new HashMap<Integer, String>();
            BufferedReader br = new BufferedReader(new FileReader("C:/Users/sgoetz/Desktop/movieReviews.txt"));
            String line = br.readLine();
            while (line != null) {
                line = br.readLine();
                System.out.println(map);
            }
        }

Upvotes: 0

Views: 5616

Answers (5)

Ashot Karakhanyan
Ashot Karakhanyan

Reputation: 2830

A more example:

while (true) {
    // Number line
    String value = br.readLine();
    if (value == null || value.trim().isEmpty()) {
        break;
    }
    Integer valueInt = Integer.parseInt(value);

    // Name line
    String title = br.readLine();
    if (title == null || value.trim().isEmpty()) {
        break;
    }

    map.put(valueInt, title);
}
System.out.println(map);

And the output is:

{1=movie title, 3=movie title, 4=movie title}

Upvotes: 0

Sarz
Sarz

Reputation: 1976

here we go File:

3,movie title,rating,other,other
4,movie title,rating,other,other
1,movie title,rating,other,other

code:

public static void main(String[] args) throws IOException {
            Map<Integer, String> map = new HashMap<Integer, String>();
            BufferedReader br = new BufferedReader(new FileReader("movieReviews.txt"));
            String line="";
            int i=0;
            while (line != null) {
                line = br.readLine();
                map.put(i,line);
                i++;
            }
            String movieNumber="";
            String movieTitle="";
            String movieRating="";
            String movieOther1="";
            String movieOther2="";
            for(int j=0;j<map.size();j++){
                if(!(map.get(j)== null)){
                    String[] getData=map.get(j).toString().split("\\,");
                    movieNumber = getData[0];
                    movieTitle = getData[1];
                    movieRating = getData[2];
                    movieOther1 = getData[3];
                    movieOther2 = getData[4];
                    System.out.println("|"+movieNumber+"|"+movieTitle+"|"+movieRating+"|"+movieOther1+"|"+movieOther2);
                }
            }
        }

Upvotes: 0

cerberus
cerberus

Reputation: 561

If you want to store moving names of against its rating you have to declare map as Map>. Following code populates the movie title against its rating. Hope this is helpful.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

public class MovieReview {

    public static void main(String[] args) {
            Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
            BufferedReader br = new BufferedReader(new FileReader("C:/Users/sgoetz/Desktop/movieReviews.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);
            }
        }
    }
}

Upvotes: 0

A4L
A4L

Reputation: 17595

Your code is printing nothing because you are printing the map on which you put nothing. So the map remains empty. Also your first while iteration is buggy, you read one line from the stream before the while loop then you enter it an immediately read the next line. The first line is lost.

For reading from a buffered stream the following pattern should be considered:

while(null != (line = br.readLine())) {
    // do what ever you want with the line.
}

Upvotes: 0

Sarz
Sarz

Reputation: 1976

Try This

public static void main(String[] args) throws IOException {
            Map<Integer, String> map = new HashMap<Integer, String>();
            BufferedReader br = new BufferedReader(new FileReader("movieReviews.txt"));
            String line="";
            int i=0;
            while (line != null) {
                line = br.readLine();
                map.put(i,line);
                i++;
            }
            for(int j=0;j<map.size();j++){
                System.out.println(map.get(j));
            }
        }

Upvotes: 1

Related Questions