Question Solved1 Answer 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 < arr.length; i++) { // check if word already exist in array if (arr[i].equalsIgnoreCase(word[j])) { // increase the count if (index >=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)

HCCYQU The Asker · Computer Science

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 < arr.length; i++) {


// check if word already exist in array
if (arr[i].equalsIgnoreCase(word[j])) {

// increase the count
if (index >=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:

Transcribed Image Text: 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)
More
Transcribed Image Text: 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)
See Answer
Add Answer +20 Points
Community Answer
HLW4NW The First Answerer
See all the answers with 1 Unlock
Get 4 Free Unlocks by registration

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Administrator */ public class Homework8 { public static void countWord(String text, PrintWriter outputFile) { /*method countWord() * input: * word:the array of words found in text * wordCOunt: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: return total words 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;changed // word[0]=arr[0];//changed //wordCount[0]=1;//changed for (int i = 0; i &lt; arr.length; i++) {//changed boolean isFound = false;//changed String wrd = arr[i]; //check if word already exist in array for (int k = 0; k &lt; word.length; k++) { if (wrd.equalsIgnoreCase(word[k])) { isFound = true; break; } } if (!isFound) { word[index] = wrd; ... See the full answer