Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Writing a Java program and I need it to read through a file and find all the cities that contain the word Wax, and it

Writing a Java program and I need it to read through a file and find all the cities that contain the word Wax, and it does that just fine. But now I need it to output the name associated with that city, but I keep getting an error. Here is my code so far.

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

class A { 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 name; 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("\\|"); name = s.split("|\\");

for (String word : words) { if (word.equals(input)) //Search for the given word { System.out.println(name); } } }

fr.close();

} }

personnel_addresses2.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 David Bash|Versailles Boyko Michels|Washington Zakary Conley|Richmond Robert Doremus|Louisville John Netherton|Wax Beau Rogers|Willisburg

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions