Question: 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
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
What I have so far:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class H4 {
public static void main(String[] args) { File File1 = new File("words_no_duplicates-edited.txt"); Scanner fileInput = null; try { fileInput = new Scanner(File1); } catch (FileNotFoundException e) { } LinkedList
while (fileInput.hasNext()) { String letter = fileInput.next(); lists[letter.charAt(0)-'a'].add(letter); } Scanner scnr = new Scanner(System.in); System.out.println("Enter a lowercase letter: "); char chara = scnr.next().charAt(0); while(!"0".equals(chara)){ System.out.println("Enter a lowercase letter: "); System.out.println(lists[chara-'a']); } } }
Need help fixing the loop where it prompts the user to enter a character I want it to be able to enter a character and once the results are returned I want it to ask the user again to enter a character or enter " 0 " to exit the program. I also want to be able to enter upper or lowercase characters I currently only have lowercase
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
