Question: * This Program inputs a list of words from a Text file and separates it with whitespace * It puts each word from the Txt

* This Program inputs a list of words from a Text file and separates it with whitespace
* It puts each word from the Txt file into an array with a count of how many times it appears
* When completed, the array is output into a text file named input3.out
*/
public class Word
{
String word;
int count;
public Word(String word) {
this.word = word;
this.count = 1;
}
}
*_____
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.util.Arrays;
import java.util.Scanner;// provide input from the keyboard
public class WordAnalyzer
{
public static Word[] loadWordsFromFile(File file)
throws FileNotFoundException {
// scanner to read data from file
Scanner scanner = new Scanner(file);
// This will create a word array with maximum capacity of 35000 words to hold the values from the text file
Word[] wordsList = new Word[35000];
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();
// This checks to see if the 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 {
//if word already exists, update 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;
}
private static int indexOf(String word, Word[] list, int size) {
// looping through array
for (int i = 0; i
if (list[i].word.equalsIgnoreCase(word)) {
// found
return i;
}
}
// not found
return -1;
}
public static void saveToFile(File file, Word[] list)
throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file) ) ;
for (int i = 0; i
writer.write(list[i].word + " : " + list[i].count + " ") ;
writer.newLine();
}
writer.close();
}
public static void main(String[] args) throws IOException {
/**
* This section is created to read input and output file names
*/
Scanner scanner = new Scanner(System.in); // Generate input from the keyboard
System.out.print("Enter the input file name: ");
String inputFileName = scanner.nextLine();
System.out.print("Enter the output file name: ");
String outputFileName = scanner.nextLine();
File infile = new File(inputFileName);
File outfile = new File(outputFileName);
try {
//this loads the words from the file
Word[] wordList = loadWordsFromFile(infile);
//this saves to the output file
saveToFile(outfile, wordList);
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
UPDATE: I cannot send the files.txt file through anything. Its too large for copy paste, snapshot, and i cant attach it. It has over 30,000 words. Can I send it to your email?
If I can't send it to you, here is what I am able to post. Hopefully it gives you a better idea:
Small contents of files.txt:
Aesop's Fables Translated by George Fyler Townsend
The Wolf and the Lamb
WOLF, meeting with a Lamb astray from the fold, resolved not to
lay violent hands on him, but to find some plea to justify to the
Lamb the Wolf's right to eat him. He thus addressed him:
"Sirrah, last year you grossly insulted me." "Indeed," bleated
the Lamb in a mournful tone of voice, "I was not then born." Then
said the Wolf, "You feed in my pasture." "No, good sir," replied
the Lamb, "I have not yet tasted grass." Again said the Wolf,
"You drink of my well." "No," exclaimed the Lamb, "I never yet
drank water, for as yet my mother's milk is both food and drink
to me." Upon which the Wolf seized him and ate him up, saying,
"Well! I won't remain supperless, even though you refute every
one of my imputations." The tyrant will always find a pretext for
his tyranny.
The Bat and the Weasels
A BAT who fell upon the ground and was caught by a Weasel pleaded
to be spared his life. The Weasel refused, saying that he was by
nature the enemy of all birds. The Bat assured him that he was
not a bird, but a mouse, and thus was set free. Shortly
afterwards the Bat again fell to the ground and was caught by
another Weasel, whom he likewise entreated not to eat him. The
Weasel said that he had a special hostility to mice. The Bat
assured him that he was not a mouse, but a bat, and thus a second
time escaped.
It is wise to turn circumstances to good account.
Can you please help me modify my existing code? I have a Java code which currently inputs a list of words from a text file (called files.txt) separated by whitespace, blank, or tab. It puts each word into an array with a count of how many times the word appears in the text. Once it's finished with the input file, it outputs the array to a text file named Input3.out. Can you please show me how to modify my current code to do the following: Modify the code to implement the list of words from the text file as a sorted doubly-linked list. So, basically, as a new word is added to the list, search the list and find where it belongs in the list. If it doesn't exist, add it in the correct location and set the word counter to 1. Example 1: Need to put "lion" in the list. The existing list is: lamb-> lunch ion is >than lamb but
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
