这为什么会导致运行时错误?(使用 HashMap 查找字符串的单词计数)

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

Why is this causing a runtime error? (finding word count of a string using HashMap)

问题

import java.util.HashMap;
import java.util.Iterator;

class L6ex1pt2 {
	String line = "当我在那里时,它也在那里"; //适用于所有唯一单词的字符串,但不适用于有重复单词的字符串(如此)

	public static void main(String[] args) {
		L6ex1pt2 test = new L6ex1pt2();
		HashMap<String, Integer> hash = new HashMap<String, Integer>();
		hash = test.countWords(test.line);
	}

	public String[] words(String s) {
		String[] words = s.split("\\s+");
		return words;
	}

	public HashMap<String, Integer> countWords(String s) {
		HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
		String[] werds = words(s);
		for (String w : werds) {
			if (wordCount.containsKey(w)) {
				wordCount.put(w, wordCount.get(w) + 1);
			} else {
				wordCount.put(w, 1);
			}
		}
		return wordCount;
	}
}

此代码部分已被翻译。

英文:
import java.util.HashMap;
import java.util.Iterator;

class L6ex1pt2 {
	String line = &quot;When I was there, it was there&quot;; //works for Strings with all unique words 
                                                    //but not for Strings with repeated words (like this)

	public static void main(String[] args) {
		L6ex1pt2 test = new L6ex1pt2();
		HashMap&lt;String, Integer&gt; hash = new HashMap&lt;String, Integer&gt;();
		hash = test.countWords(test.line);

	}

	public String[] words(String s) {
		String[] words = s.split(&quot;\\s+&quot;);           
		return words;
	}

	public HashMap&lt;String, Integer&gt; countWords(String s) {
		HashMap&lt;String, Integer&gt; wordCount = new HashMap&lt;String, Integer&gt;();
		String[] werds = words(s);
		for (String w: werds) {
			if (wordCount.containsKey(w)) {
				wordCount.put(w, wordCount.get(s)+1);
			} else {
				wordCount.put(w, 1);                
			}
		}
		return wordCount;
	}

}

Throws this exception:

Exception in thread "main" java.lang.NullPointerException
at L6ex1pt2.countWords(L6ex1pt2.java:24)
at L6ex1pt2.main(L6ex1pt2.java:10)

答案1

得分: 1

Line 25

wordCount.put(w, wordCount.get(s)+1);

应更改为

wordCount.put(w, wordCount.get(w)+1);

因为变量 s 包含整个字符串,而不是您期望用作哈希映射键的单词。

英文:

Line 25

wordCount.put(w, wordCount.get(s)+1);

should be

wordCount.put(w, wordCount.get(w)+1);

Since s contains the whole string and not the word you expect for the hashmap key.

答案2

得分: 0

在你的第24行代码中,你错误地写成了:
wordCount.put(w, wordCount.get(s)+1);

应该是:

wordCount.put(w, wordCount.get(w)+1);

英文:

In your line number 24, you have wrongly written:
wordCount.put(w, wordCount.get(s)+1);

It should be:

wordCount.put(w, wordCount.get(w)+1);

答案3

得分: 0

get(s)+1 替换为 get(w)+1

完整代码:

import java.util.HashMap;
import java.util.Iterator;

public class Main {

    String line = "When I was there it was there"; 

    public static void main(String[] args) {

        Main test = new Main();
        HashMap<String, Integer> hash = new HashMap<String, Integer>();
        hash = test.countWords(test.line);
        System.out.println(hash);
    }

    public HashMap<String, Integer> countWords(String s) {
        HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
        String[] wordsArr = words(s);
        for (String w: wordsArr) {
            if (wordCount.containsKey(w)) {
                wordCount.put(w, wordCount.get(w)+1);
            } else {
                wordCount.put(w, 1);                
            }
        }
        return wordCount;
    }

    public String[] words(String s) {
        String[] words = s.split("\\s+");           
        return words;
    }
}

输出:

{When=1, there=2, was=2, I=1, it=1}
英文:

Replace get(s)+1 with get(w)+1

Full code :

import java.util.HashMap;
import java.util.Iterator;

public class Main {
    
    String line = &quot;When I was there it was there&quot;; 
    
    public static void main(String[] args) {
		
		Main test = new Main();
        HashMap&lt;String, Integer&gt; hash = new HashMap&lt;String, Integer&gt;();
        hash = test.countWords(test.line);
        System.out.println(hash);
	}

    public HashMap&lt;String, Integer&gt; countWords(String s) {
        HashMap&lt;String, Integer&gt; wordCount = new HashMap&lt;String, Integer&gt;();
        String[] wordsArr = words(s);
        for (String w: wordsArr) {
            if (wordCount.containsKey(w)) {
                wordCount.put(w, wordCount.get(w)+1);
            } else {
                wordCount.put(w, 1);                
            }
        }
        return wordCount;
    }
    
    public String[] words(String s) {
        String[] words = s.split(&quot;\\s+&quot;);           
        return words;
    }
}

Output :

{When=1, there=2, was=2, I=1, it=1}    

huangapple
  • 本文由 发表于 2020年7月21日 19:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63013387.html
匿名

发表评论

匿名网友

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

确定