-1
import java.util.HashMap;
import java.util.Iterator;

class L6ex1pt2 {
    String line = "When I was there, it was there"; //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<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(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)

sgkduffi
  • 1
  • 1
  • Is `wordCount.put(w, wordCount.get(s)+1);` line 24? In that case, when did you put anything in `wordCount.get(s)`? – Federico klez Culloca Jul 21 '20 at 11:23
  • Yeah, its on line 24. It's supposed to skip that line if it doesn't find w in wordCount - have just realized that s is supposed to be a w. Working fine now, thanks for your help – sgkduffi Jul 21 '20 at 11:48

3 Answers3

1

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.

Greedo
  • 3,438
  • 1
  • 13
  • 28
Julin Alex
  • 56
  • 3
0

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);

KnockingHeads
  • 1,569
  • 1
  • 22
  • 42
0

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

Full code :

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;
    }
}

Output :

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

                                                                                                               
Som
  • 1,522
  • 1
  • 15
  • 48