Question: I am new to Java and I am trying to write a small program and the first part of the program needs to read a

I am new to Java and I am trying to write a small program and the first part of the program needs to read a text file and search for the word "Wax" in it. I found this code online but it is not working it keeps outputting that the word is not present in the text file when there should be over 13,000 instances of the word in the text file.

Code:

package File;

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException;

public class FileWordSearch { public static void main(String[] args) throws IOException { File f1=new File("personnel_addresses2.txt"); //Creation of File Descriptor for input file String[] words=null; //Intialize the word Array FileReader fr = new FileReader(f1); //Creation of File Reader object BufferedReader br = new BufferedReader(fr); //Creation of BufferedReader object String s; String input="Wax"; // Input word to be searched int count=0; //Intialize the word to zero while((s=br.readLine())!=null) //Reading Content from the file { words=s.split("|"); //Split the word using space for (String word : words) { if (word.equals(input)) //Search for the given word { count++; //If Present increase the count by one } } } if(count!=0) //Check for count not equal to zero { System.out.println("The given word is present for "+count+ " Times in the file"); } else { System.out.println("The given word is not present in the file"); }

fr.close(); }

}

personnel_addresses.txt(partial):

Japheth Edwards|Albany Evan Taylor|Wax Olivia Grimme|Wax Alfonso McNeil|Somerset Allyson Schroeder|Washington April Tomes|Lancaster Laura Hancock|Louisville Robert Joyce|Remington HamedHaribHamed Yates|Somerset Robin Snoke|Remington

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!