Write a program that loads the given dictionary into an ArrayList. Afterwards, a
ID: 3754527 • Letter: W
Question
Write a program that loads the given dictionary into an ArrayList. Afterwards, ask the user for a word. Display the position in the list where the word occurs. I wasn't able to insert the dictionary file but i'm sure any dictionary file will work for this.
1st Test Case:
Standard Input:
hello
Required Output:
21882 words were loaded.
What word are you looking for?
"hello" is at index 8897
Search again?
2nd Test Case:
Standard Input:
hello
yes
world
yes
zip code
no
Required output:
21882 words were loaded.
What word are you looking for?
"hello" is at index 8897
Search again?
What word are you looking for?
"world" is at index 21688
Search again?
What word are you looking for?
"zip code" is at index -1
Search again?
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Dictionary.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Dictionary {
//array list to store the words
static ArrayList<String> dictionary = new ArrayList<String>();
//method to load words from a file to the array list
static void loadWords(String filename) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(filename));
dictionary.clear(); //clearing current values, if any
//looping through the file, adding all words to the dictionary
while (scanner.hasNext()) {
//converting word into lowercase before adding to the array list
String word = scanner.next().toLowerCase();
dictionary.add(word);
}
}
public static void main(String[] args) {
//scanner to read user input
Scanner scanner = new Scanner(System.in);
try {
//loading words from a file called dictionary.txt
//make sure you have a file named dictionary.txt in the project root folder
loadWords("dictionary.txt");
//displaying number of words loaded
System.out.println(dictionary.size()+" words were loaded");
String choice = "yes", word;
//looping until user inputs no
while (choice.equalsIgnoreCase("yes")) {
System.out.println("What word are you looking for: ");
//getting word
word = scanner.nextLine();
//checking if dictionary contains the word
if (dictionary.contains(word.toLowerCase())) {
//found, displaying the index
System.out.printf(""%s" is at index %d ", word,
dictionary.indexOf(word.toLowerCase()));
}else{
//not found
System.out.printf(""%s" is not found in the dictionary ", word);
}
//prompting again
System.out.println("Search again? (yes/no): ");
choice=scanner.nextLine();
}
} catch (FileNotFoundException e) {
System.out.println("dictionary.txt file is not found!");
}
}
}
/*OUTPUT (using my dictionary.txt file)*/
1000 words were loaded
What word are you looking for:
hello
"hello" is at index 996
Search again? (yes/no):
yes
What word are you looking for:
follow
"follow" is at index 175
Search again? (yes/no):
yes
What word are you looking for:
great
"great" is at index 131
Search again? (yes/no):
yes
What word are you looking for:
Java
"Java" is not found in the dictionary
Search again? (yes/no):
no
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.