Question: can someone please fix this!! i need help with the input file.txt and output file.txt. Make sure the output is eactly the one in the
can someone please fix this!! i need help with the input file.txt and output file.txt.
Make sure the output is eactly the one in the assignement. i need help with the indent
my code:
import java.util.Scanner; import java.io.*; import java.util.Arrays;
public class homework8 { public static void countWord(String text, PrintWriter outputFile) { /* method countWord() * Input: * word: the array of words found in text. * wordCount: the array of counted words found in text. * homework8input: the file for input text. * homework8output: the file for output text * Process: * Counts occurrences of word found in file * Output: * returns totalwords found in the list. */ text = text.toLowerCase(); String arr[] = text.split(" "); Arrays.sort(arr); // Array to store different words String[] word = new String[arr.length]; // Array to store different words count int[] wordCount = new int[arr.length]; int index = 0; String getWord; word[0] = arr[0]; wordCount[0] = 1; while (inputFile.hasNext()) { line = inputFile.nextLine(); for (int i = 1, j = 0; i =0) { wordCount[index] +=1; } else { j++; // store to the word array word[j] = arr[i]; // initialize count to 1 wordCount[j] = 1; } } // Write content to file outputFile.println(word[i] + " " + wordCount[i]); } outputFile.close(); } public static void main (String[] args) throws IOException { //Open the input file File myFile = new File("homework8input.txt"); //Create a Scanner object to read the input file Scanner inputFile = new Scanner(myFile); //Create an output file object using the PrintWriter Class PrintWriter outputFile = new PrintWriter("homework8output.txt"); //PrintWriter oututFile = new PrintWriter(System.out); String text; //outputFile.println("Enter the text: "); text = inputFile.nextLine(); countWord(text, outputFile); } }
input:
The elephant ate the banana and the giraffe ate the banana
the assignment:

Homework Topic 8 Word Count: Write a program that reads in and prints a text, line by line, and calls a series of methods as needed. The object of the program is to parse a sentence in order to figure out all the different words that are in the sentence and how many times each word occurs. Use two parallel arrays, one to store the different words (e.g., String[] word) and one to store the appearance count for each word (e.g., int[] wordCount). At the end, print the list of different words and their counts. (Extra Credit: print the list of words and their counts in alphabetical order.) You have to decide which methods to implement. For example, suppose the text is this: The elephant ate the banana and the giraffe ate the banana. The output would produces this list: N NP and ate banana elephant giraffe the 1 2 2 1 1 4 Hint: Think Bank Accounts! (Use what you learned in the Bank Accounts program.) Required Submission: 1. The Java source code file (e.g., HW8.java) 2. The data input file containing the sentence to be parsed (e.g., input.txt) 3. The program generated output file (e.g., output.txt)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
