Hi all i have a question, i have to find how many times words in a file repeat, so i write a code like this
public class CountWords {
FileReader fr;
ArrayList<String> wordsList = new ArrayList<>();
public CountWords(String fname) {
try {
fr = new FileReader(fname);
Scanner s = new Scanner(fr);
while(s.hasNextLine()){
String[] parola = s.nextLine().split("[\\p{Blank}]|[\\p{Punct}&&\\p{Blank}]|[\\p{Punct}]");
for (int i = 0; i < parola.length;){
if (parola[i] == " "){
i ++;
}else {
wordsList.add(parola[i]);
i++;
}
}
}
s.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public List<String> getResult() {
ArrayList<String> res = new ArrayList<String>();
HashMap<String,Integer> set = new HashMap<>();
for(String slowa : wordsList) {
set.put(slowa, set.containsKey(slowa) ? set.get(slowa) + 1 : 1 );
}
for(String key : set.keySet()){
res.add(key + " " + set.get(key));
}
return res;
}
}
After the last try where i put a lot of Puncts in between the words i have the problem that the output give me in the array "parola" the blank spaces too how to not include the blank spaces in the array?