Question

Please make a method in Java that will allow me to find a String in a...

Please make a method in Java that will allow me to find a String in a file using recursion. The details are the following:

Search the input file line by line for a given string. The output must contain the line number, followed by the contents of the line that contains the search argument. For instance given the following the search string: Java, the program would search the file line by line generating a result such as the following:

50: on the island of Java

95: The people of JAVA loves jaVa.

Use recursion to search the file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SearchFileRecursive {

    public static void search(Scanner fin, String word, int lineNumber) {
        if (fin.hasNextLine()) {
            String line = fin.nextLine();
            if (line.toLowerCase().contains(word.toLowerCase())) {
                System.out.println(lineNumber + ": " + line);
            }
            search(fin, word, lineNumber+1);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter file name: ");
        File file = new File(in.nextLine());
        try {
            Scanner fin = new Scanner(file);
            System.out.print("Enter a word to search for: ");
            String word = in.nextLine();
            search(fin, word, 1);
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " does not exists!");
        }
        in.close();
    }
}
Add a comment
Know the answer?
Add Answer to:
Please make a method in Java that will allow me to find a String in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • java find and replace code pls help me We write code that can find and replace...

    java find and replace code pls help me We write code that can find and replace in a given text file. The code you write should take the parameters as command line arguments: java FindReplace -i <input file> -f "<find-string>" -r "<replace-string> -o <output file> *question mark(?) can be used instead of any character. "?al" string an be sal ,kal,val *In addition, a certain set of characters can be given in brackets.( "kng[a,b,f,d,s]ne" string an be kngane,hngbne,kangfne,kangdne,kangsne So, all you...

  • Please help me with this question. It is in java and if possible please provide details....

    Please help me with this question. It is in java and if possible please provide details. Thank you so much. With this question there is no file provide, just make one up. Write a program that reads words from a text file and displays all the words(duplicates allowed) in ascending alphabetical order. The words must start with a letter. The text file is passed as a command-line argument.

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • Design "MyWeek" class: (1) Data members: the class contains one private data field weekList. "wee...

    Design "MyWeek" class: (1) Data members: the class contains one private data field weekList. "weekList" has datatype of ArrayList, which will contain String objects of week/weekend days. There are no duplicated days in the list. Valid days are "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun". The weekList is not necessarily to be ordered (2) Constructors The instance of MyWeek can be constructed with (a) no argument (initialized weekList to empty), or (b) with one String argument which is one valid...

  • Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input...

    Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Num of passengers^range^name^manufacturer^model where: Num of passengers is the total number of people in the plane. This represents an integer number, but remember that it is still part of the String so...

  • For this problem, you have to use following hash function: key modulo the number of buckets....

    For this problem, you have to use following hash function: key modulo the number of buckets. Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT