Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 54 additions & 1 deletion src/WordFrequency.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
* This class reads through a text file and counts the number
* of times a word occurs in the text file - uses Map, Scanner, and Linked Map classes.
*
* @author Jasmine David
*/
public class WordFrequency {

public static void main(String[] args) {
System.out.println("Hello world!");
Map<String, Integer> wordCounts = new TreeMap<>();
String maxWord = "";
int maxCount = 0;

// create scanner to read the file
try(Scanner inFile = new Scanner(new File("tinyTale.txt"))) {

// grab next word in file and save in String called word
while (inFile.hasNext()) {
String word = inFile.next();

// check to see if the word is in the map
if (wordCounts.containsKey(word)) { // if in the table
// get the existing value out
int value = wordCounts.get(word);
// increment the value
value++;
// update/put the updated value back in
wordCounts.put(word, value);

// Find word with the highest frequency count
if (value > maxCount) {
maxCount = wordCounts.get(word);
maxWord = word;
}

} else { // if not in table
// add the word to the map with a value of 1
wordCounts.put(word, 1);
}
} // end of the while loop

System.out.println(wordCounts);
System.out.println(maxWord + " appeared the most at " + maxCount + " times");


}
catch (FileNotFoundException e){
System.out.println("ERROR - File Not Found");
}

}
}