将文本文件数据放入哈希映射中

时间:2014-02-25 19:55:12

标签: java java-io

我在文本文件中的数据看起来像这样......

3
movie title
4
movie title
1
movie title

顶部的数字是电影评级,其下的文字是电影标题。

我到目前为止的代码如下。但除了空支架外,它不打印任何东西!示例代码将不胜感激!

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

5 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

您的代码没有打印任何内容,因为您正在打印您没有放置任何内容的地图。所以地图仍然是空的。你的第一次迭代也是错误的,你在while循环之前从流中读取了一行,然后输入它,立即读取下一行。第一行丢失了。

要从缓冲流中读取,应考虑以下模式:

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

答案 2 :(得分:0)

如果您想存储与其评级相对应的移动名称,您必须将地图声明为地图&gt;。以下代码根据其评级填充电影标题。希望这有用。

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

答案 3 :(得分:0)

我们走了 文件:

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

代码:

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

答案 4 :(得分:0)

更多例子:

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

输出是:

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