Question

MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of...

MUST BE IN JAVA AND USE RECURSION

Write a recursive method that returns the number of all occurrences of a given word in all the files under a directory. Write a test program. Use the following header:

public static long findInFile(File file, String word)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static long findInFile(File file, String word) throws FileNotFoundException {
    return findInFileHelper(new Scanner(file),word);
}

public static long findInFileHelper(Scanner scan, String word){
    if(scan.hasNext()){
        if(scan.next().equals(word)){
            return 1+findInFileHelper(scan,word);
        }
        else{
            return findInFileHelper(scan,word);
        }
    }
    else{
        return 0;
    }
}
Add a comment
Know the answer?
Add Answer to:
MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of...
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
  • Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions...

    Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive a zero for that problem....

  • 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)...

  • Using listAllFiles as a guide, write a method that has one File argument: if the argument...

    Using listAllFiles as a guide, write a method that has one File argument: if the argument is a directory, the method returns the total number of files below it. If the argument represents a file, the method just returns 1. public static int countFiles(File f)   import java.io.File; public class FileLister { public static void main(String[] args) { // Choose the directory you want to list. // If running in Eclipse, "." will just be the current project directory. // Use...

  • write a java program :- Use a recursive method that can take an arbitrarily long string...

    write a java program :- Use a recursive method that can take an arbitrarily long string and reverse the order of its characters. Example: "Real men don't eat quiche." would be ".ehciuq tea t'nod nem leaR" must use RECURSION to solve this challenge. make sure to comment the code thoroughly.

  • this is a java course. the topic is a recursive topic. Please, very importantly write the...

    this is a java course. the topic is a recursive topic. Please, very importantly write the testing program in another file. A file for the program and another file for testing the program. Thanks in advance Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method. 3.

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • 10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use...

    10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...

  • **JAVA** Write a binary search method for a String array that might contain nulls. Your method...

    **JAVA** Write a binary search method for a String array that might contain nulls. Your method should not crash or throw a runtime exception. See the driver program for examples. The method header is: public static int binarySearchWithNulls(String[] words, String target) must be recursive.

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

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