The problem I think is in overall design, the program was initally running and compiling correctly. I then tried to slightly complicate matters by using a seperate method to declare the Arraylist and store the infirmation from the textfile. Now because the dictionary and userInput strings are all linked inside the getDictionary() method the program is on continuous loop. Other than a redraft is there any obvious direction to take this?
Java Code:
import java.util.*;
import java.io.*;
public class MayTuesday {
public static void main(String args[]) throws Exception {
getDictionary();
}
public static ArrayList<String> getDictionary() throws Exception {
ArrayList<String> dictionary = new ArrayList<String>(); //declare a String ArrayList object "dictionary"
Scanner fileReader = new Scanner (new File("dictionary.txt")); //Scanner object to read from dictionary.txt
while (fileReader.hasNext()) // while loop to read from dictionary.txt
dictionary.add(fileReader.nextLine()); // Adds the next String to the dictionary ArrayList
System.out.println("Enter the word to spell check: ");
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.nextLine();
if(dictionary.contains(userInput)) {
int index = dictionary.indexOf(userInput);
System.out.println(userInput+":"+"\t Is spelled correctly and was found at index "+ index +" of the ArrayList successfully.");
} else {
System.out.println("Word was spelled incorrectly");
}
return getDictionary();
}
}
No comments:
Post a Comment