I am trying to write a method, AddWordsInCorpus, which would use the strings for each entry in the ConcurrentHashMap called lemmas( get the strings stored in the values), split them by space into separate words, and then add these words into an ArrayList named corpus.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import helpers.JSONIOHelper;
public class DescriptiveStatistics {
private static void StartCreatingStatistics(String filePath) {
// create an object of the JSONIOHelper class
JSONIOHelper JSONIO = new JSONIOHelper();
JSONIO.LoadJSON(filePath); /
ConcurrentHashMap<String, String> lemmas = JSONIO.GetLemmasFromJSONStructure();
private void AddWordsInCorpus(ConcurrentHashMap<String, String> lemmas) {
//compile the words in the corpus into a new ArrayList
ArrayList<String> corpus = new ArrayList<String>();
// a loop to get the values (words) and add them into corpus.
for(Entry<String, String> entry : lemmas.entrySet()){
String[] words = entry.getValue(); // error 1
for(String word : lemmas.split(" ")) { // error 2
corpus.addAll(Arrays.asList(word));}
}
I am getting the following two errors:
Error 1. Type mismatch: cannot convert String to String[]
Error 2. //The method split(String) is undefined for the type ConcurrentHashMap<String,String>
Can anybody help me with this?
CodePudding user response:
entry.getValue ()
returns a string not a string array:
String words = entry.getValue(); // error 1
and this string can be splitted:
for(String word : words.split(" ")) { // error 2
CodePudding user response:
Values in your map ConcurrentHashMap<String, String> lemmas
are of type String
not String[]
and that's why you are seeing error 1.
Second error is beacause you are trying to split your HashMap lemmas.split(" ")
and of course, you can't do that.
If I understand you correctly, your code should look like this:
for (Entry<String, String> entry : lemmas.entrySet()) {
String word = entry.getValue(); // error 1
String[] words = word.split(" ");
for (String w : words) { // error 2
corpus.addAll(w);
}
}