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

I looked at your files, now I don't understand your question!

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?

You already have that file

I dont know how to create or access this file through the main.

The code already has methods to read the file into memory, and check if a given word is present.

I know that I have to use equals or == to compare the input

The code contains an example of exactly how to do that

Maybe you need to read that code very carefully again - it seems to contain everythung you need.

Finally, when you have code to post, include it in your message and enclose it on CODE tags (highlight all the code and click the CODE format item above the message edit area). That way people can see the code easily, and with comments, constants etc colour-coded.

@JamesCherrill agree on code in post and I move it there ;)

I figured out the issue. I created a file through the java project by right clicking on src, then new, then file creating a "words" file with all of the dictionary words. Since this did not work, I went to the actually java project location(through the C drive) and copied the "words" file into the project folder instead of through eclipse. So now there is no error and so far so good. Please allow me 1-2 days (or even much less) from the time this was posted to try a code the remaining code. Within this time interval, if I (or others) enter no more questions or concerns I will mark this thread as solved. Thanks!
The code is much easier to read in the code bracket format than in black and white :).

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();
	        int counter =0;
	        int tries = 0;
	        
	        String userInput = ""; 
	      //  String userInput2 =""; // stores user Input in main loop
	     String wordStart = "grade";
	     String wordEnd = "iced";
	        // Display identifying information
	        System.out.println( " ");
	        System.out.println(wordStart);
	        System.out.println(wordEnd);
	        
	   	        while(userInput != wordEnd){
	      //  System.out.print("Enter a word to lookup: ");
	        	
	        	//if(wordExists(userInput)){ 
	        	System.out.println("next word:");
	        	userInput = keyboard.next();
	        // convert to all lower case
	        userInput = userInput.toLowerCase();
	        if( wordExists( userInput)) {
	       //     System.out.println("Next Word");
	        }
	        else {
	           System.out.println("No, " + userInput + " is NOT in the dictionary. "+ "TRY AGAIN!");                
	        }
	        if(userInput.equals(wordEnd)){
	    		 System.out.println("Congratulations you did it!");
	    		 System.out.println("GAME OVER");
	    		 break;
	    	 }
	        }
	        System.out.println("\n" +"Exiting program...\n");
	        }
	    //end main()

Okay so I figured out the basics. Now what I want to do is make it more user friendly. Can someone explain how I would go about checking that the new valid word entered is only one character different from previous word, either a letter was changed, removed, or added. The game would be more interesting if it could pick a set combination of words automatically, instead of me entering a set pair(valid starting and ending word pairs were found here http://wordmorph.sarangconsulting.com/). I was thinking of an array or multiple variables with a random number generator that would pick a number corresponding to a pair? Thanks!

Thanks!

Just start writing a simple method eg
public static boolean isValidChange(String word1, String word2) { ...
You have 3 cases to test for - change, add, remove a letter. Code and test those 1 at a time. Start with 1 letter changed, that's probably the easiest.

Okay Thanks I will try that. As for my other question regarding the random selection of a pair of words, I no longer need help with that I figured it out.

Loop thru both words simultaneously (compare word1char1 with word2char1 etc) counting how many times the corresponding chars are the same - total should be length-1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.