用于映射的Java Buffered Reader

时间:2017-04-21 10:38:37

标签: java bufferedreader

我试图从一个存储如下的文件中读取测验;

在第一行显示标识符和问题标识符,然后是问题标题,然后是问题标题,请回答带答案的标题,最后选择答案。

我试图读取文件,然后使用HashMap存储它

16 TF
Question
Because an ArrayList is an indexed collection, you can access its elements using a subscript. 
Answer
False
True
Selected
1

258 MC
Question
Fill in the blank. A Node is generally defined inside another class, making it a(n) ____ class.
Answer 
Private 
Inner 
Public 
Internal 
Selected 
2

37 L5
Question
How would you rate your programming skills? 
Answer
Excellent
Very good
Good
Not as good as they should be
Poor
Selected
-1

我的代码:

  Quiz newquiz = new Quiz();
    List<String> newArray = new ArrayList<String>();
    Map<Integer, String> newquizmap = new HashMap<Integer, String>();
    BufferedReader BufferedReader = new BufferedReader(new FileReader('file.txt'));
    String line = null;

    while ((line = BR.readLine()) != null) {
        String nuquiz[] = line.split("   ");
        BufferedReader.readLine();

       newquiz.newquestionid = Integer.parseInt(parts[0]);



             lines.add(line);
               newquizmap.put(newquiz.newquestionid, newArray.toString());


            line = BufferedReader.readLine();

            System.out.println(newquizmap);
        } 

    } BR.close();

我知道这不对,我甚至认为它很接近,但是我真的在努力解决这个问题,任何人都可以给我任何帮助吗?

编辑:

          Quiz newquiz = new Quiz();
    List<String> newArray = new ArrayList<String>();
    Map<Integer, String> newquizmap = new HashMap<Integer, String>();
    BufferedReader BufferedReader = new BufferedReader(new FileReader('file.txt'));
    String line = null;

    while ((line = BR.readLine()) != null) {
        String nuquiz[] = line.split("   ");
        BufferedReader.readLine();

       newquiz.newquestionid = Integer.parseInt(parts[0]);



             lines.add(line);
               newquizmap.put(newquiz.newquestionid, newArray.toString());


            line = BufferedReader.readLine();

            System.out.println(newquizmap);
        } 

    } BR.close();

尝试过这段代码,它似乎用每个值填充每个地图键?

1 个答案:

答案 0 :(得分:1)

一种方法是使用java对象作为hashmap值。创建一个新类以包含以下属性(修改以处理所有问题用例):

private String questionId; //This will hold question Id - TF    
private String question; //This will hold the Question - Because an ArrayList is an indexed collection, you can access its elements using a subscript. 
    private List<String> choices; //This will hold the choices - Private Inner Public Internal 
    private Integer selectedChoice; //This will hold the answer

在类中包含参数化构造函数和其他必需的方法。

解析文件,遍历内容,创建这个新类的对象,并添加到hashmap,其中“id”作为键,“pojo object”作为值。

注意:请修复java代码中的一些其他问题(格式,编码标准,如以大写,QuestionSet,StdOut等开头的变量名)