Question

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
* by the rest of the words in the set, space delimited.
* Note: The String class indexOf method can be used to find the :. The keyword is
* everything prior to the : and the keywords are space delimited after.
*
* An error message to System.out should be printed and an empty list returned
* in the following cases:
* A set number is not found.
* "Error: set " + fileSetNumber + " not found."
* A set doesn't have the : after the keyword.
* "Error: colon (:) not found in set " + fileSetNumber + "."
*
* Example file format:
* about: about auto bat boat but out tab tub
* other: her hero hot other the toe
* their: her hire hit the their tie tier tire
*
* If the set number is 2 the returned ArrayList should contain:
* [other, her, hero, hot, other, the, toe]
*
* @param filename The file containing word sets.
* @param set The number of the set, starting with 1.
* @return The set of words, with the keyword first and later in the list.
* @throws FileNotFoundException
*/
public static ArrayList<String> loadWordSet(String filename, int set) throws FileNotFoundException {
return null; //TODO
}
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Program

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Test {
   public static void main(String[] args) throws FileNotFoundException {
       Scanner sc = new Scanner(System.in);
      
       System.out.print("Enter filename: ");
       String filename = sc.nextLine();
       System.out.print("Enter set number: ");
       int set = sc.nextInt();
       sc.close();
      
       ArrayList<String> list = loadWordSet(filename, set);
      
       if(list != null) {
           System.out.println("The list of words : "+list);
       }
   }

   public static ArrayList<String> loadWordSet(String filename, int set) throws FileNotFoundException {
       Scanner sc = new Scanner(new File(filename));
       ArrayList<String> list = new ArrayList<>();
       String str = "";
       boolean check = true;

       // checking if the line number exists or not
       for (int i = 1; i <= set; i++) {
           check = sc.hasNext();

           // handling exception if no line found
           try {
               str = sc.nextLine();
           } catch (NoSuchElementException e) {
           }
       }

       sc.close();

       if (!check) {
           System.out.println("Error: set " + set + " not found");
           return null;
       }

       // checking colon
       String[] ar = str.split(" ");
       if (ar[0].indexOf(':') == -1) {
           System.out.println("Error: colon (:) not found in set " + set + ".");
           return null;
       }

       // removing colon
       ar[0] = ar[0].replaceFirst(":", "");

       // adding words to list
       for (String i : ar) {
           list.add(i);
       }
       return list;
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
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...
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
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