Write a program that allows the user to search for a given word ina text file. T
ID: 3610118 • Letter: W
Question
Write a program that allows the user to search for a given word ina text file. The two inputs are the file's name and the targetword. If the target is not found, the program outputs a message tothat effect. Otherwise, the program outputs the number of timesthat this word occurs in the file and the position where it isfirst encountered(counting from position 0). The program shouldignore case when it compares words. Java Programming Use JOptionPane when asking or showing a message. Also ask if you want to play or repeat the program again.Make the text file name great. Have the text file say( Its great that classes can call text files.) Java Programming Use JOptionPane when asking or showing a message. Also ask if you want to play or repeat the program again.
Make the text file name great. Have the text file say( Its great that classes can call text files.)
Explanation / Answer
please rate - thanks import java.io.*; import java.util.*; import javax.swing.*; public class stringsearch {public static void main(String[] args)throwsFileNotFoundException {String word,yorn="Y", want,str,filename; int first,count,index; while(yorn.charAt(0)=='Y'||yorn.charAt(0)=='y') {filename=JOptionPane.showInputDialog("Enter file name to search:"); try { Scannerinput=new Scanner(new FileReader(filename)); want=filename=JOptionPane.showInputDialog("Enter word to search:"); want=want.toUpperCase(); count=0; index=-1; first=-1; while(input.hasNext()) {index++; word=input.next(); word=word.toUpperCase(); if(want.equals(word)) {count++; if(firstRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.