ava Programming This week's project involves a text file with that holds all of the novel Don Quixote*, by Miguel Cervantes. The text file has been heavily processed. It contains only lowercase letters, spaces, and new lines. That is a good format for counting words. Write code that reads through the text file one word at a time using a Scanner, and the next() method. Put the words into a HashMap, where the words are used as keys, and the values are Integers used to keep track of how many times the words occur in the text. If the text was "one fish two fish red fish blue fish one two three" then the HashMap would look like this: key value "one".... 2 "fish"... 4 "two".... 2 "red".... 1 "blue"... 1 "three".. 1 Once you have created such a HashMap for the whole of the text you can write logic that prints out the answers to these questions: How many times does each of these words occur in the text: "don", "quixote", "panza", "dulcinea", "impossible", "dream" What word occurs most often in the text COMPLETE CODE: import java.util.Iterator; import java.util.Map; import java.util.HashMap; import java.util.Arrays; class Main { public static void main(String[] args) { final String INPUT_FILE_NAME = "DonQuixote.txt"; // Read the file one word at a time. // Use a HashMap to count all of the words // Find the most common word // Count the uses of each of these words: // }
Java Programming
This week's project involves a text file with that holds all of the novel Don Quixote*, by Miguel Cervantes. The text file has been heavily processed. It contains only lowercase letters, spaces, and new lines. That is a good format for counting words. Write code that reads through the text file one word at a time using a Scanner, and the next() method.
Put the words into a HashMap, where the words are used as keys, and the values are Integers used to keep track of how many times the words occur in the text.
If the text was "one fish two fish red fish blue fish one two three" then the HashMap would look like this:
key value
"one".... 2
"fish"... 4
"two".... 2
"red".... 1
"blue"... 1
"three".. 1
Once you have created such a HashMap for the whole of the text you can write logic that prints out the answers to these questions:
How many times does each of these words occur in the text: "don", "quixote", "panza", "dulcinea", "impossible", "dream"
What word occurs most often in the text
COMPLETE CODE:
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
final String INPUT_FILE_NAME = "DonQuixote.txt";
// Read the file one word at a time.
// Use a HashMap to count all of the words
// Find the most common word
// Count the uses of each of these words:
//
}
Step by step
Solved in 5 steps with 3 images