Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A passage of text is rated for difficulty by the length of the words it contains

ID: 3843622 • Letter: A

Question

A passage of text is rated for difficulty by the length of the words it contains. You are to complete the class WordLengthFinder to group words by their length.

You are given a string. Create a Scanner to process the String. You are to print the words with the shortest words first. Within words of the same length, you are to print the words alphabetically.

You can use a Map where the keys are the Integer length and the values are the Set of the words of that length. This is similar to hw 17.

You can print the Set mySet like this: System.out.println(mySet);

For the string "Mary had a little lamb", the output would look like this

In order to get words without following punctuation, create a scanner for the String and set the delimiter to use: scan.useDelimiter("[^A-Za-z0-9]+");

Codecheck will run your program more than once. Note that the second string is fairly long and you will have to scroll right to see expected and actual output.

----------------------------------------------------

Complete the following file:

WordLengthFinder.java

import java.util.TreeSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class WordLengthFinder
{
public static void main(String[] args)
{
String passage = "mary had a little lamb";
}

}

Explanation / Answer

import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
*
* @author Sam
*/
public class WordLengthFinder {
    public static void main(String[] args) {
        String passage = "Mary had a little lamb";
        Scanner scan = new Scanner(passage);
        scan.useDelimiter("[^A-Za-z0-9]+");
        TreeMap<Integer, TreeSet<String>> treeMap = parsePassage(scan);
        while (!treeMap.isEmpty()) {
            System.out.println(treeMap.pollFirstEntry().getValue()); //pollFirstEntry removes the entry with lowest value of key. We print then Value part of the Entry.
        }
    }
   
    private static TreeMap<Integer, TreeSet<String>> parsePassage(Scanner scan) {
        Map<Integer, TreeSet<String>> treeMap = new TreeMap<>();
        while (scan.hasNext()) {
            String s = scan.next(); //get every word
            int length = s.length(); //get length of the word
            if (treeMap.containsKey(length)){ //checks if the set of that length is already present in set
                Set<String> set = treeMap.get(length); //get the saved set
                set.add(s); //add new word to the set
            }
            else { //case when that length is not present
                Set<String> set = new TreeSet<>(); //create a new set
                set.add(s); //add word to new set
                treeMap.put(length, (TreeSet<String>) set); //add the new set to tree
            }
        }
        return (TreeMap<Integer, TreeSet<String>>) treeMap;
    }
}

This code will serve your purpose. I hope you understand the useage of Set and Map in java. You can go though the comments as they can help you understand the coding logic well.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote