From what you can see in my code, I got the file read using a pattern as a delimiter to omit punctuation and white spaces. The problem that I have is that I don't know how to use collections.. well actually I know how to use it's methods, but I can't completely grasp the concept.
What kind of collection would allow me to keep a list of unique words and a number of occurrences for each one of them? If I could use a two-dimensional array this assignment would be easy, but alas the teacher didn't go into too much detail with collections...
Thank you.
import java.io.*;
import java.util.Scanner;
public class WordOccurrence {
Scanner sc;
public static void main(String[] args) {
new WordOccurrence();
}
WordOccurrence() {
/** File setup */
try {
sc = new Scanner(new File("/Volumes/Data/Code/Eclipse workspace/project4/src/Obama_Education_Speech.txt"));
readFile(sc);
} catch (FileNotFoundException e){
System.out.println("Error: File not found.");
}
}
private void readFile(Scanner sc) {
sc.useDelimiter("\\s|[,.'?\"]");
while (sc.hasNext()) {
System.out.println(" << " + sc.next() + " >> ");
}
}
}

New Topic/Question
Reply



MultiQuote





|