(Intro to java help?) Complete the class code below and include a method named \
ID: 3706371 • Letter: #
Question
(Intro to java help?)
Complete the class code below and include a method named "search" that takes two String parameters, a string to search and the other a target string to search for in the first string. The search method will return an integer representing the number of times the search string was found. The string being searched is a super-string formed by joining all lines of a text file, so we might expect to get multiple hits with our method.
HINT: Walk the String in a loop and use <string>.substring(start, end) to examine a target-sized segment of the input string for equality to your target word.
public class WordSearch {
public static void main(String[] args) {
String fileName = "story.txt";
String target = "help";
Scanner fileIn = new Scanner(new File(fileName));
String story = "";
while(fileIn.hasNextLine()) {
story += fileIn.nextLine(); // Build a super-String
}
System.out.println("(" + target + ") found " + search(story, target) + " times");
}
// method code here
} // end of WordSearch class
Explanation / Answer
below is the solution:
package chegg;
import java.io.*;
import java.util.Scanner;
public class WordSearch {
public static void main(String[] args) throws IOException{
String fileName = "D:/story.txt"; //file that has to read
String search; //search variable to search in a file
Scanner inputString = new Scanner(System.in); //inputString for input to search a string
Scanner inputFile = new Scanner(System.in); //for file input
inputFile = new Scanner(new File(fileName)); //take a file
System.out.println("Enter String to search: "); //ask the user to input a string to search
search = inputString.next(); //scanner input taken from user
String story = ""; //set storystring to null
while(inputFile.hasNextLine()) { // read string from file line by line and add into story string
story += inputFile.nextLine(); // Build a super-String
}
System.out.println(search+" found " + search(story, search) + " times"); //prints the number of occurance of string
}
//seacrh method for search a string
private static int search(String story, String seacrString) {
String strWithoutSpace = story.replaceAll(" ", ""); //replace the space in string
int lastIndex = 0; //delcare the last index for the last of the string
int count = 0; //count for number of count
while (lastIndex != -1) { //loop to the last string
lastIndex = strWithoutSpace.indexOf(seacrString, lastIndex); //search the string
if (lastIndex != -1) { //check if the string found
count++; //count the number
lastIndex += seacrString.length(); //position to the last string
}
}
return count; //return the count
}
}
story.txt file:
You
can
search
for
usages
of
a
single
word
or
a
combination
of
words.
It gives you sample sentences and tells you how often each word is used, so you can see what is the most common way to use a word. I was introduced to this website in a writing course about academic writing and I've been using it ever since.
sample output:
Enter String to search:
a
a found 19 times
Enter String to search:
and
and found 2 times
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.