Write a Java program that inputs a list of words from a text file, separated by
ID: 3744628 • Letter: W
Question
Write a Java program that inputs a list of words from a text file, separated by whitespace (blank or tab, and the end of line) Put each word into an array with a count of how many times the word appears. When finished with the input file, output the array to a text file named HW1.out Guidelines 1) Do not hard code the file names in the program. Ask the user for the file names, both input and output. 2) Check to make sure the input file exists! 3) If either a single quote or double quote appears, consider it part of the word, e.g. it's, it and it's are three different words. 4). Once you find a word, convert to lower case before putting it in the array -no need to worry about capitalization 5) Create a small text file to test your program. By hand, count the number of times a word appears. This is the test plan to confirm that the program works. 6) Do Not use ArrayList. Create your own data structure. 7) Create the class files with stubs. Compile and verify at each step the program worksExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Word.java
/**
* class to represent a single word
*/
public class Word {
String word; // word
int count; // number of occurrences
public Word(String word) {
this.word = word;
this.count = 1;
}
}
// WordAnalyzer.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class WordAnalyzer {
/**
* method to read a file, extract all the words and return an array
* containing all words and their counts
*
* @param file
* - input file
* @return Word object array
* @throws FileNotFoundException
* if file not found
*/
public static Word[] loadWordsFromFile(File file)
throws FileNotFoundException {
// scanner to read data from file
Scanner scanner = new Scanner(file);
// creating a Word array with maximum capacity of 1000 words
// assuming the file has less than 1000 unique words
Word[] wordsList = new Word[1000];
int count = 0; // count of total words
// looping through all words
while (scanner.hasNext()) {
// extracting word, converting to lower case
String w = scanner.next().toLowerCase();
// checking if word is already in the array
int index = indexOf(w, wordsList, count);
if (index == -1) {
// word doesnt exist, adding as the new word
Word word = new Word(w);
wordsList[count] = word;
count++; // total word count
} else {
// word already exists, updating the count of that word
wordsList[index].count++;
}
}
// shrinking the array so that it does not have any null values
wordsList = Arrays.copyOf(wordsList, count);
return wordsList;
}
/**
* method to check if a word exists in an array of Words.
*
* @param word
* - String word
* @param list
* - Word array
* @param size
* - number of non null elements in array
* @return index if found, else -1
*/
private static int indexOf(String word, Word[] list, int size) {
// looping through array
for (int i = 0; i < size; i++) {
if (list[i].word.equalsIgnoreCase(word)) {
// found
return i;
}
}
// not found
return -1;
}
/**
* method to write words and their counts to an output file
*
* @param file
* - output file
* @param list
* - Word array
* @throws FileNotFoundException
* if output file cant be opened
*/
public static void saveToFile(File file, Word[] list)
throws FileNotFoundException {
PrintWriter writer = new PrintWriter(file);
for (int i = 0; i < list.length; i++) {
// appending in 'Word : Count' format
writer.println(list[i].word + " : " + list[i].count);
}
writer.close();
}
public static void main(String[] args) {
/**
* Reading input, output file names
*/
Scanner scanner = new Scanner(System.in);
System.out.print("Enter input file name: ");
String inputFileName = scanner.nextLine();
System.out.print("Enter output file name: ");
String outputFileName = scanner.nextLine();
File infile = new File(inputFileName);
File outfile = new File(outputFileName);
try {
//loading words from file
Word[] wordList = loadWordsFromFile(infile);
//saving to output file
saveToFile(outfile, wordList);
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
/*OUTPUT*/
Enter input file name: words.txt
Enter output file name: HW1.out
/*HW1.out after running program*/
where : 1
do : 1
random : 1
thoughts : 1
come : 1
from? : 1
the : 10
waves : 1
were : 3
crashing : 1
on : 1
shore; : 1
it : 2
was : 2
a : 4
lovely : 1
sight. : 1
please : 1
wait : 1
outside : 1
of : 3
house. : 1
i : 3
love : 1
eating : 1
toasted : 1
cheese : 1
and : 3
tuna : 1
sandwiches. : 1
rock : 1
music : 1
approaches : 1
at : 1
high : 1
velocity. : 1
there : 2
white : 1
out : 1
conditions : 1
in : 2
town; : 1
subsequently, : 1
roads : 1
impassable. : 1
yeah, : 1
think : 1
it's : 1
good : 2
environment : 1
for : 2
learning : 1
english. : 1
sometimes : 1
is : 3
better : 2
to : 5
just : 1
walk : 1
away : 1
from : 1
things : 1
go : 2
back : 1
them : 1
later : 1
when : 1
you’re : 1
frame : 1
mind. : 1
he : 2
didn’t : 1
want : 2
dentist, : 1
yet : 1
went : 1
anyway. : 1
christmas : 1
coming. : 1
they : 2
got : 2
early, : 1
really : 1
seats. : 1
more : 1
detailed : 1
information. : 1
wednesday : 1
hump : 1
day, : 1
but : 1
has : 1
anyone : 1
asked : 1
camel : 1
if : 1
he’s : 1
happy : 1
about : 1
it? : 1
body : 1
may : 1
perhaps : 1
compensates : 1
loss : 1
true : 1
metaphysics. : 1
she : 1
too : 1
short : 1
see : 1
over : 1
fence. : 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.