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

This exercise uses the Java LinkedList class. Then prompt the user for a (lower

ID: 3750959 • Letter: T

Question

This exercise uses the Java LinkedList class.

Then prompt the user for a (lower case) character and display all the words beginning with that character. (If the user enters an invalid character, trap them in a loop until they give you a valid one).

Note: nothing is sorted. I do NOT want to see 26 if statements or 26 case Switch statements. Use your head and remember ASCII tables.

EXTRA credit: Allow the user to enter any alphabetic character (lower or upper case). You will then need to convert it to lower case.

Explanation / Answer

PROGRAM :

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedList;
import java.util.Scanner;
public class WordReader {
private static final String FILENAME = "E:\chegg\words_no_duplicates.txt";
private static Map<String, LinkedList<String>> listMap = new HashMap<String, LinkedList<String>>();
public static void main(String[] args) {
boolean isWordFound = false;
WordReader.readFile();
Scanner scanner = new Scanner(System.in);
try {
while (!isWordFound) {
System.out.println("Enter character in lower case");
String firstChar = scanner.next();
LinkedList<String> list = WordReader.listMap.get(firstChar);
if (list != null && !list.isEmpty()) {
System.out.println(" List of words beginning with "
+ firstChar + " : " + list.toString());
isWordFound = true;
} else {
System.out
.print(" Word starts with entered character not found. ");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null)
scanner.close();
}
}
public static void readFile() {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
if (WordReader.listMap.containsKey("" + sCurrentLine.charAt(0))) {
WordReader.listMap.get("" + sCurrentLine.charAt(0)).add(
sCurrentLine);
} else {
LinkedList<String> word = new LinkedList<String>();
word.add(sCurrentLine);
WordReader.listMap.put("" + sCurrentLine.charAt(0), word);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

Output
-----------
Enter character in lower case
z
Word starts with entered character not found. Enter character in lower case
d
List of words beginning with d : [drenching, dybbuk, dovecote]

Description
------------------
1. We need to run main method of WordReader class given above to get the output.
2. I have taken Map<key,Value> to store the list of words. key is first letter of word and value is list of all the words which starts from that first letter.
3. when we want to get the list of words which starts from specific character, we get the same by key into the map.
Let me know if you need more assistence on above code. Thanks

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