Java 缓冲 Reader 映射
Java Buffered Reader to map
我正在尝试 reader 从如下存储的文件中进行测验;
第一行是标识符和问题标识符,然后是问题标题和下一个问题,答案标题和答案,最后是所选答案。
我正在尝试读取文件,然后使用 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();
试过这段代码,它似乎用每个值填充每个映射键?
一种方法是使用 java 对象作为 hashmap 值。创建一个新的 class 以包含以下属性(修改以处理您所有的问题用例):
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
在 class.
中包含参数化构造函数和其他必需的方法
解析文件,遍历内容,创建这个新 class 的对象并添加到以 "id" 为键和 "pojo object" 为值的 hashmap。
注意:请修复您的 java 代码中的其他一些问题(格式、编码标准,例如以大写开头的变量名称、QuestionSet、StdOut.. 等)
我正在尝试 reader 从如下存储的文件中进行测验;
第一行是标识符和问题标识符,然后是问题标题和下一个问题,答案标题和答案,最后是所选答案。
我正在尝试读取文件,然后使用 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();
试过这段代码,它似乎用每个值填充每个映射键?
一种方法是使用 java 对象作为 hashmap 值。创建一个新的 class 以包含以下属性(修改以处理您所有的问题用例):
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
在 class.
中包含参数化构造函数和其他必需的方法解析文件,遍历内容,创建这个新 class 的对象并添加到以 "id" 为键和 "pojo object" 为值的 hashmap。
注意:请修复您的 java 代码中的其他一些问题(格式、编码标准,例如以大写开头的变量名称、QuestionSet、StdOut.. 等)