HELP IN JAVA PLEASE: This exercise uses the Java LinkedList class. Using the inp
ID: 3751919 • Letter: H
Question
HELP IN JAVA PLEASE:
This exercise uses the Java LinkedList class.
Using the input file words_no_duplicates-edited.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. I do NOT want to see 26 if statements or 26 case Switch statements. Use your head and remember ASCII tables.
Allow the user to enter any alphabetic character (lower or upper case). You will then need to convert it to lower case.
words_no_duplicates-edited.txt:
noncollectable
reallocation
drenching
obnoxious
venality
dybbuk
shotgun
changelessly
handiwork
unheralded
dovecote
anode
spellbind
psychologist
improvisational
prejudiced
apply
pokey
secular
masterfully
at
overdrawn
costar
scruff
awareness
impurely
Rubric:
* use of try/catch(File reader only (File I/O))
*use of while loop to collect input
use of array of LinkedLists
*correctly put string into the right list. zero points for 26 statements or 26 cases in switch statement
*prompt user for input
*display correct list
*allow user to enter upper case or lower case character
Explanation / Answer
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.