Need help filling in the code in JAVA, ONLY edit the parts that are bolded: Cont
ID: 3737529 • Letter: N
Question
Need help filling in the code in JAVA, ONLY edit the parts that are bolded: Contents of Wordlist txt is at the bottom
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
* UniqueWords - Write a program that uses a TreeSet to print each
* unique word from a file alphabetically
* Based upon P16.1 in Big Java
*/
public class UniqueWords
{
public static void main(String[] args) throws FileNotFoundException
{
Set <String> words = readWords("wordlist.txt");
for(String word : words)
System.out.println(word);
}
/**
Reads all words from a file and puts them into a set (a tree set)
@param filename the name of the file
@return a set with all lowercased words in the file. Here, a
word is a sequence of upper- and lowercase letters.
*/
//-----------Start below here. To do: approximate lines of code = 6
// 1. static method readWords returning a set of Strings ... FileNotFoundException
{//opening of the method
//2. let 'words' be a set of Strings, constructed as a TreeSet
//3. let 'scanner' be based on a file with name 'filename'
//4. let scanner useDelimiter any run of one or more (+) characters
//other than a-z or A-Z
//5. while scanner has another word
//6. Put the lower case version of that word into 'words'.
return words; //returning the set of words from file 'filename'
}//closing of the method
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
----------------------------------------------------------------
Wordlist.txt contents:
The TreeSet class arranges its elements in sorted order.
In order to use a TreeSet, the element should implement the Comparable
interface.
You can also construct a TreeSet with a Comparator.
Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; /** * UniqueWords - Write a program that uses a TreeSet to print each * unique word from a file alphabetically * Based upon P16.1 in Big Java */ public class UniqueWords { public static void main(String[] args) throws FileNotFoundException { Set words = readWords("wordlist.txt"); for(String word : words) System.out.println(word); } /** Reads all words from a file and puts them into a set (a tree set) @param filename the name of the file @return a set with all lowercased words in the file. Here, a word is a sequence of upper- and lowercase letters. */ //-----------Start below here. To do: approximate lines of code = 6 // 1. static method readWords returning a set of Strings ... FileNotFoundException public static Set readWords(String filename) throws FileNotFoundException { //opening of the method //2. let 'words' be a set of Strings, constructed as a TreeSet Set words = new TreeSet(); //3. let 'scanner' be based on a file with name 'filename' Scanner scanner = new Scanner(new File(filename)); //4. let scanner useDelimiter any run of one or more (+) characters // other than a-z or A-Z scanner.useDelimiter("[^a-zA-z]+"); //5. while scanner has another word while(scanner.hasNext()) { //6. Put the lower case version of that word into 'words'. words.add(scanner.next().toLowerCase()); } scanner.close(); return words; //returning the set of words from file 'filename' }//closing of the method //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.