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:
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 < arr.length; i++) {//changed boolean isFound = false;//changed String wrd = arr[i]; //check if word already exist in array for (int k = 0; k < word.length; k++) { if (wrd.equalsIgnoreCase(word[k])) { isFound = true; break; } } if (!isFound) { word[index] = wrd; ... See the full answer