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; // for the number of occurrences
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
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
// appending in 'Word : Count' format
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);
}
}
}
Can you please help me modify my existing code? I have my 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 it to implement the list of words from the text file as a sorted doubly-linked list. 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 Example1 Need to put "lion" in the list The existing list is lamb-> lunch lion is > that lamb but lunch lion is > that lamb butStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started