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

HELP IN JAVA: This exercise uses the Java LinkedList class. Using the input file

ID: 3886299 • Letter: H

Question

HELP IN JAVA:

This exercise uses the Java LinkedList class.

Using the input file words_no_duplicates.txt, input each string putting it into a different LinkedList depending on the first character in the String. (Yes, you will need 26 linked lists).

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.

words_no_duplicates.txt:

noncollectable

reallocation

drenching

obnoxious

venality

dybbuk

shotgun

changelessly

handiwork

unheralded

dovecote

anode

spellbind

psychologist

improvisational

prejudiced

Explanation / Answer


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