Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that reads an input file containing several lines of text.

ID: 3804160 • Letter: W

Question

Write a Java program that reads an input file containing several lines of text. The

program should store each word or phrase using Collection strategies and provide the

user with an interface to search the input text.

Your program should implement the following functionality:

1. Prompt the user to enter the name and location of the input file. User input:

“c://project//input.txt”

2. List each words or phrases in the input file together with the line numbers of

their appearance and their frequency of occurrence. For exapmle:

SAMPLE INPUT:

A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element. The "root" pointer points to the topmost node in the tree.

OUPUT

Word Frequency Lines

A 4 1, 2

Binary 2 1

Tree 2 1, 2

3. List all lines of text by their line #.

4. Allow the user to enter a word or phrase to search. The users search must

retrievable for the duration of the program’s execution

a. Programming //Exact Match

b. Prog* //Matches all words that start with Prog

c. *ing //Matches all words that end with ing

d. *gram* //Matches all words that contain the sequence “gram”

e. Pro*ing //Matches all words that start and end with “Pro” and “ing”

f. “I love programming” //Matches the phrase

5. The user’s search should display the following:

a. A listing of all words that match.

b. The frequency of each match.

c. The line number of each matching word

d. The matching lines of text from the input file.

Explanation / Answer

import java.util.*;

import java.io.*;

//Class to hold the count and line numbers of words
class H {
int count = 0;
//Set to hold line numbers
Set<Integer> al;

H() {
count = 0;
al = new HashSet();
}
}

public class Main {

public static void main(String[] args) throws FileNotFoundException, IOException {
//Reader for collecting user input
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the file location");
String file = obj.readLine().trim();
  
//Reader to read the file
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
int line = 1;
HashMap<String, H> hm = new HashMap();
  
//Reading the words from file
while ((str = br.readLine()) != null) {
String s[] = str.split(" ");
for (String x : s) {
if (!hm.containsKey(x)) {
hm.put(x, new H());
}
H t = hm.get(x);
t.count++;
t.al.add(line);
}
line++;
}
Set<String> key = hm.keySet();
String word;
System.out.println("Enter the word to search for");
word = obj.readLine();
if (word.contains("*")) {
if (word.endsWith("*")) {
int ind = word.indexOf('*');
System.out.println("word is " + word);
word = word.substring(0, ind);
for (String x : key) {
if (x.startsWith(word)) {
System.out.println(x + " " + hm.get(x).count + " " + hm.get(x).al);
}
}
} else if (word.startsWith("*")) {
int ind = word.indexOf('*');
while (word.charAt(ind) == '*') {
ind++;
}
word = word.substring(ind);
for (String x : key) {
if (x.endsWith(word)) {
System.out.println(x + " " + hm.get(x).count + " " + hm.get(x).al);
}
}
} else {
int ind = word.indexOf('*');
String first = word.substring(0, ind);
while (word.charAt(ind) == '*') {
ind++;
}
String last = word.substring(ind);
for (String x : key) {
if (x.startsWith(first) && x.endsWith(last)) {
System.out.println(x + " " + hm.get(x).count + " " + hm.get(x).al);
}
}
}
}
else if(word.trim().split(" ").length==1){
if(hm.containsKey(word.trim())){
H h = hm.get(word.trim());
System.out.println("Freq "+h.count+" lines "+h.al);
}
}
else {
br = new BufferedReader(new FileReader(file));
line = 1;
while ((str = br.readLine()) != null) {
if (str.contains(word)) {
System.out.println("At line " + line);
}
line++;
}
}
}
}

OUTPUT 1 :

Enter the file location
P:/file.txt
Enter the word to search for
pointer
Freq 2 lines [1, 3]

OUTPUT 2 :

Enter the file location
P:/file.txt
Enter the word to search for
poi*
word is poi*
pointer, 4 [1, 3]
points 2 [1, 3]
pointer 2 [1, 3]

OUTPUT 3 :

Enter the file location
P:/file.txt
Enter the word to search for
*s
contains 2 [1, 3]
points 2 [1, 3]
is 3 [1, 2, 3]
nodes 1 [2]

OUTPUT 4 :

Enter the file location
P:/file.txt
Enter the word to search for
p*s
points 2 [1, 3]

OUTPUT 5 :

Enter the file location
P:/file.txt
Enter the word to search for
and a data element
At line 1
At line 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote