Here’s the code:
Scanner scan = new Scanner(new FileReader ("C:\\mytext.txt"));
HashMap<String, Integer> listOfWords = new HashMap<String, Integer>();
while(scan.hasNextLine())
{
Scanner innerScan = new Scanner(scan.nextLine());
boolean wordExistence ;
while(wordExistence = innerScan.hasNext())
{
String word = innerScan.next();
int countWord = 0;
if(!listOfWords.containsKey(word)){ already
listOfWords.put(word, 1);
}else{
countWord = listOfWords.get(word) + 1;
listOfWords.remove(word);
listOfWords.put(word, countWord);
}
}
}
System.out.println(listOfWords.toString());
The problem is, my output contains words like :
document.Because=1
document.This=1
space.=1
How do I handle this full stop’s that are occuring?(And for further issues, I think any sentence terminator would be an issue, like question mark or exclamation mark).
Take a look at the class notes for the
Scanner API, in particular the paragraph on using delimiters other than whitespace.