Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* This Java Program inputs a list of words from a Text file and separates it wit

ID: 3750845 • Letter: #

Question

* 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 < size; 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 < list.length; 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

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


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) {
insertSorted(w, wordsList, count);
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 void insertSorted(String word, Word[] list, int size){
int index = 0;
//find the correct place to insert
for(int i = 0; i < size; i++)
{
if(word.compareTo(list[i].word) < 0) //found the correct place
{
index = i;
break;
}
}
//move all other elements one position to right to make space for the new word
for(int i = size; i >= index; i--)
{
list[i+1] = list[i];
}
//now save the new word in the correct index found
list[index] = new Word(word);
}
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;
}
public static void saveToFile(File file, Word[] list)
throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < list.length; 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);
}
}
}