I need the program to be able to ask the user to input more strings after they h
ID: 3907194 • Letter: I
Question
I need the program to be able to ask the user to input more strings after they have answered once. The program right now just ends after inputing a string. When asked to input more strings the user shouldn't have to put in the data file again they should just be asked to "Enter a word to search for in the file."
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SearchFile extends File
{
public SearchFile(String arg0)
{ //default constructor to set the file name
super(arg0);
}
public int search(String word) throws FileNotFoundException
{
Scanner obj=new Scanner(this);//read from file pointed to by this object
int count=0;
while(obj.hasNextLine())
{//while there are still words left to read from file
String wr=obj.nextLine();//get the next word from file
if(wr.contains(word))//if word is equal to the word we are searching for
count++;//increment the count
}
return count;//return the count
}
public static void main(String[] args) throws FileNotFoundException
{
Scanner obj=new Scanner(System.in);//create a scanner object to read console input
System.out.println("Enter the file name to search for.");
String file=obj.nextLine();//get the string file name
System.out.println("Enter a word to search for in the file.");
String word=obj.nextLine();//get the word to search
SearchFile search=new SearchFile(file);//create an object of searchfile class
int count=search.search(word);//get the count of occurrence of word
//print the final result
System.out.println(word+" was found "+count+" times in the file "+search.getName());
}
}
Explanation / Answer
I have used a loop for this case. I will ask the user if they want to continue or not. If they type no, then the program stops else the program asks for the word to search.
I have not provided the output of my code as I didn't have the File class code and the file you're using.
CODE:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SearchFile extends File
{
public SearchFile(String arg0)
{ //default constructor to set the file name
super(arg0);
}
public int search(String word) throws FileNotFoundException
{
Scanner obj=new Scanner(this);//read from file pointed to by this object
int count=0;
while(obj.hasNextLine())
{//while there are still words left to read from file
String wr=obj.nextLine();//get the next word from file
if(wr.contains(word))//if word is equal to the word we are searching for
count++;//increment the count
}
return count;//return the count
}
public static void main(String[] args) throws FileNotFoundException
{
Scanner obj=new Scanner(System.in);//create a scanner object to read console input
System.out.println("Enter the file name to search for.");
String file=obj.nextLine();//get the string file name
String want_to_continue = "yes";
while(want_to_continue != 'no')
{
System.out.println("Enter a word to search for in the file.");
String word=obj.nextLine();//get the word to search
SearchFile search=new SearchFile(file);//create an object of searchfile class
int count=search.search(word);//get the count of occurrence of word
//print the final result
System.out.println(word+" was found "+count+" times in the file "+search.getName());
System.out.println("Do you want to continue? Enter yes to continue and no to exit");
want_to_continue = obj.nextLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.