Code attached in the attachment is not my own and was supplied by an instructor.
I am to create a word morphing game. The computer will provide a
starting word and an ending word. I am to enter a new word, that either adds or
removes a letter from the previous word. An example of game play would be:
ending word: iced
starting word: grade
next word: grace
next word: race
next word: ruce
ERROR! word not found in dictionary, please continue from the word race
next word: ace
next word: ice
next word: iced
Congratulations you did it!
The code that is attched needs an interface to the dictionary(a seperate list of all the dictionary "words"). there is a file called "words," that is the dictionary.
I have a few questions but for now, I need to be able to reference the dictionary file (attached called "words"). So how do I create a file with a list of all the words that can be accessed by an user input to verify if the word exists?
I know that I have to use equals or == to compare the input to the word but I dont know how to create or access this file through the main.(sorry for any confusion)
IF ANY OF THE FORMATTING OR PRESENTATION IS INCORRECT PLEASE LET ME KNOW SO I CAN CORRECT MYSELF FOR FUTURE POST. SORRY AND THANKS IN ADVANCE.
// import Java libraries that are needed
import java.util.Scanner; // used for console input
// the following are needed to implement reading from the dictionary
import java.io.IOException; // for IOException
import java.util.ArrayList; // Used to create ArrayLists dictionary use
import java.io.*; // Used for IOException, File
// Declare the class
public class wordLookup
{
// Fields that can be accessed anywhere in the class go here
Scanner keyboard = new Scanner( System.in); // used to read user input
// Declare a dynamically allocated ArrayList of Strings for the dictionary.
// The dictionary can hold any number of words.
ArrayList<String> dictionary;
//--------------------------------------------------------------------------
// main() - startup main loop. It is necessary to create an instance of this
// class and then call a method from that instance, otherwise there may
// be error messages having to do with non-static objects (e.g. keyboard)
// being called from a static context (e.g. main).
// The words "throws IOException" have to do with dictionary error
// handling.
//
public static void main(String[] args) throws IOException
{
// create an instance of this class
wordLookup DecryptInstance = new wordLookup();
// call a non-static method to do everything else
DecryptInstance.mainLoop();
}
//-------------------------------------------------------------------------
// mainLoop() - display identifying information and run main loop with menu
// The words "throws IOException" have to do with dictionary error
// handling.
//
void mainLoop() throws IOException
{
// First take care of creating and initializing the dictionary
// Define the instance of the dictionary ArrayList
dictionary = new ArrayList<String>();
// Now fill the dictionary array list with words from the dictionary file
readInDictionaryWords();
String userInput = ""; // stores user Input in main loop
// Display identifying information
System.out.println( "Author: ");
// Add your code below this, but do change the ID information above ...
// ...
// The code below is just a sample of how to look up a word
System.out.print("Enter a word to lookup: ");
userInput = keyboard.next();
// convert to all upper case
userInput = userInput.toLowerCase();
// lookup each word and print message telling if it is found
if( wordExists( userInput)) {
System.out.println("Yes, " + userInput + " is in the dictionary. ");
}
else {
System.out.println("No, " + userInput + " is NOT in the dictionary. ");
}
System.out.println("\n" +"Exiting program...\n");
}//end main()
// ************** Don't change the methods below this line **************
// Read in the words to create the dictionary.
// It throws an IOException, which is a way to gracefully handle errors
// should there be a problem reading from the input.
public void readInDictionaryWords() throws IOException
{
// Define a Scanner to read from an input file. Note that the name of
// the file given in the code below MUST match the actual filename of
// the dictionary file. This file should be in the same directory
// as the source code for WordCross.java
File dictionaryFile = new File("words"); // declare the file
// print the directory where this program expects to find dictionary
System.out.println(System.getProperty("user.dir"));
// ensure file exists and is in the correct directory
if( ! dictionaryFile.exists()) {
System.out.println("*** Error *** \n" +
"Your dictionary file has the wrong name or is " +
"in the wrong directory. \n" +
"Aborting program...\n\n");
System.exit( -1); // Terminate the program
}
Scanner inputFile = new Scanner( dictionaryFile);
// while there are words in the input file, add them to the dictionary
while( inputFile.hasNext()) {
dictionary.add( inputFile.nextLine() );
}
}//end createDictionary()
// Allow looking up a word in dictionary, returning a value of true or false
public boolean wordExists( String wordToLookup)
{
if( dictionary.contains( wordToLookup)) {
return true; // words was found in dictionary
}
else {
return false; // word was not found in dictionary
}
}//end wordExists
}//end Class