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

 * This Java Program inputs a list of words from a

* This Java 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);

}

}

}

Can you plea sh metoity m fe 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 lion is> than lamb but

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!