英文:
How do I read in a file such that for each line that contains a certain string, save the lines underneath to a HashMap?
问题
I admit this might be a simple question but I cannot seem to map my head around it. Say that I have a .txt file that I'm reading with a BufferedReader in Java that might look like this:
Category: Dairy
Yogurt
Cheese
Butter
Category: Vegetables
Carrots
Cucumbers
Category: Snacks
Granola Bars
Trail Mix
Chips
Candy
Pop Tarts
I want to create a HashMap, for example, called groceries that will map the items to its respective category. For example:
groceries.put("item", "category");
How do I read this file in such a way that keeps track of which item belongs to which category so I can place it into the HashMap in a meaningful way? Here is a rough idea of what I have tried so far. (I tried initially with ArrayLists but figured out HashMaps might be better to map the data)
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
if (line.contains("Category")) {
// Save in an ArrayList called Category
} else {
// Save in an ArrayList called Item
}
}
Again that is just what I had originally tried. Let me know if any further clarification is needed.
英文:
I admit this might be a simple question but I cannot seem to map my head around it. Say that I have a .txt file that I'm reading with a BufferedReader in Java that might look like this:
Category: Dairy
Yogurt
Cheese
Butter
Category: Vegetables
Carrots
Cucumbers
Category: Snacks
Granola Bars
Trail Mix
Chips
Candy
Pop Tarts
I want to create a HashMap, for example, called groceries that will map the items to its respective category. For example:
groceries.put("item", "category");
How do I read this file in such a way that keeps track of which item belongs to which category so I can place it into the HashMap in a meaningful way? Here is a rough idea of what I have tried so far. (I tried initially with ArrayLists but figured out HashMaps might be better to map the data)
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
if (line.contains("Category")){
(save in an arrayList() called Category)
}
else{
(save in arrayList() called Item)
}
}
Again that is just what I had originally tried. Let me know if any further clarification is needed.
答案1
得分: 1
这可以启发(在任何人关闭它之前):
您想要构建的结构是一个映射(键-值结构),而不是一个列表。
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
String currentCategory = "";
Map<String, String> groceries = new HashMap<>();
while ((line = in.readLine()) != null) {
if (line.startsWith("Category: ")) {
currentCategory = line.substring(10);
} else {
groceries.put(line, currentCategory);
}
}
英文:
This can inspire (before anyone closes it ):
The structure you want to build is a map (key-value structure) not a list.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)));
String line;
String currentCategory = "";
Map<String, String> groceries = new HashMap<>();
while ((line = in.readLine()) != null) {
if (line.startsWith("Category: ")) {
currentCategory = line.substring(10);
} else {
groceries.put(line, currentCategory);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论