Question

[Java] I need help completing the method below. The instructions are also below. public int wordSearch(String...

[Java] I need help completing the method below. The instructions are also below.

public int wordSearch(String word, String filename) {

}
- find the first time the word appears in a text file filename and returns the position of the word in the file(if its the first word in the file, method should return 1)
- assume no punctuation in file
- assume all whitespace is either a new line or single space
- do not include the times that word appears as substring of another word in the file
- if the word is not in file, return -1
- a exception that occurs, return -1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class WordSearch {

    public static void main(String[] args) throws IOException
    {
        WordSearch w=new WordSearch();
    Scanner scnr=new Scanner(System.in);
   
    int result;
    String filename="words.txt";
     
    System.out.print("Enter word to search : ");
    String search=scnr.next();
    result=w.wordSearch(search,filename);
    System.out.println(result);
    }
    public int wordSearch(String word, String filename) throws IOException
    {
    FileInputStream fileIn = new FileInputStream(filename);
    String line = "";
    int result=0,i=0;
    Scanner stdin = new Scanner(fileIn);
  
        while(stdin.hasNextLine())
        {
            line = stdin.next();
            result=line.indexOf(word);
            i++;
            if(result==0)
            {
                break;
            }
                     
        }
        stdin.close();
        if(result!=0)
            i=-1;
   return i;
}

}

Add a comment
Know the answer?
Add Answer to:
[Java] I need help completing the method below. The instructions are also below. public int wordSearch(String...
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
  • JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) {...

    JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) {         if (index < 0 || index > numItems) {             System.out.println("Get error: Index "                         + index + " is out of bounds.");             return null;         }         return array[index]; } Hint: Update both the method signature and the method body! 2. Update the below method to include a try-catch block rather than throwing the exception to...

  • I need help with this one method in java. Here are the guidelines. Only public Employee[]...

    I need help with this one method in java. Here are the guidelines. Only public Employee[] findAllBySubstring(String find). EmployeeManager EmployeeManager - employees : Employee[] - employeeMax : final int = 10 -currentEmployees : int <<constructor>> EmployeeManager + addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) + removeEmployee( index : int) + listAll() + listHourly() + listSalary() + listCommision() + resetWeek() + calculatePayout() :...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int...

    Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int col) This method does the following: compare the first character from word to the character in puzzle[row][col] if they match subtract one from row (to check the previous character in the same column) and continue comparing characters, by advancing to the next character in word else if puzzle[row][col] is NOT part of puzzle array or the character in the cell does not match the...

  • JAVA Overview Create a method public static void spongeBobify(String inputPath, String outputPath...

    JAVA Overview Create a method public static void spongeBobify(String inputPath, String outputPath, Mode mode) that will convert a file to Mocking Sponge Bob text in 3 different modes. EveryOther capitalize the first letter of the string capitalize every other letter (ignoring non-letter character like punctuation and spaces). non-letter characters don't count toward the every other count Example: mocking sponge bob! → MoCkInG sPoNgE bOb! Vowels capitalize every vowel (not including y) Random capitalize each letter with a probability of 35%....

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Finds the specified set of words in the specified file and returns them * as an ArrayList. This finds the specified set in the file which is on the * line number of the set. The first line and set in the file is 1. * * This returns an ArrayList with the keyword first, then : and then followed...

  • Assume that you have a String "name" attribute in a Java class definition.  Write a public method...

    Assume that you have a String "name" attribute in a Java class definition.  Write a public method to return this "name" attribute. public String getName() { return name;         } 1.) Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”.​ 2.) The method getValueFromFile is public and returns an int. It accepts no arguments. The method is capable of throwing an IOException and a FileNotFoundException. Write the header for this method.

  • (9) Complete the below Java method recursively, which takes in a String and returns if it's...

    (9) Complete the below Java method recursively, which takes in a String and returns if it's a palindrome. Remember a palindrome means a string that is the same reversed, like "deed", "racecar" or "tacocat". The empty string "" is also a palindrome. public boolean isPalindrome (String word) { (8) (a) (8 points) Implement merging two sorted arrays into one sorted ar- ray. These arrays are sorted in descending order. For example, (5, 4, 2). Return an array with all the...

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