How do I read in a file such that for each line that contains a certain string, save the lines underneath to a HashMap?

huangapple go评论83阅读模式
英文:

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 = &quot;&quot;;
Map&lt;String, String&gt; groceries = new HashMap&lt;&gt;();
while ((line = in.readLine()) != null) {
	if (line.startsWith(&quot;Category: &quot;)) {
		currentCategory = line.substring(10);
	} else {
		groceries.put(line, currentCategory);
	}
}

huangapple
  • 本文由 发表于 2020年8月12日 23:29:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63379843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定