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

Edited 4 Years Ago by peter_budo: Moving code from file to post. We prefer to see it instead of downloading

Attachments
A
A's
AOL
AOL's
Aachen
Aachen's
Aaliyah
Aaliyah's
Aaron
Aaron's
Abbas
Abbasid
Abbasid's
Abbott
Abbott's
Abby
Abby's
Abdul
Abdul's
Abe
Abe's
Abel
Abel's
Abelard
Abelard's
Abelson
Abelson's
Aberdeen
Aberdeen's
Abernathy
Abernathy's
Abidjan
Abidjan's
Abigail
Abigail's
Abilene
Abilene's
Abner
Abner's
Abraham
Abraham's
Abram
Abram's
Abrams
Absalom
Absalom's
Abuja
Abyssinia
Abyssinia's
Abyssinian
Ac
Ac's
Acadia
Acadia's
Acapulco
Acapulco's
Accra
Accra's
Acevedo
Acevedo's
Achaean
Achaean's
Achebe
Achebe's
Achernar
Achernar's
Acheson
Acheson's
Achilles
Aconcagua
Aconcagua's
Acosta
Acosta's
Acropolis
Acropolis's
Acrux
Acrux's
Actaeon
Actaeon's
Acton
Acton's
Acts
Acuff
Acuff's
Ada
Ada's
Adam
Adam's
Adams
Adan
Adan's
Adana
Adana's
Adar
Adar's
Addams
Adderley
Adderley's
Addie
Addie's
Addison
Addison's
Adela
Adela's
Adelaide
Adelaide's
Adele
Adele's
Adeline
Adeline's
Aden
Aden's
Adenauer
Adenauer's
Adhara
Adhara's
Adidas
Adidas's
Adirondack
Adirondack's
Adirondacks
Adkins
Adkins's
Adler
Adler's
Adolf
Adolf's
Adolfo
Adolfo's
Adolph
Adolph's
Adonis
Adonis's
Adonises
Adrian
Adrian's
Adriana
Adriana's
Adriatic
Adrienne
Adrienne's
Advent
Advent's
Adventist
Adventist's
Advents
Advil
Advil's
Aegean
Aelfric
Aelfric's
Aeneas
Aeneid
Aeneid's
Aeolus
Aeolus's
Aeroflot
Aeroflot's
Aeschylus
Aeschylus's
Aesculapius
Aesculapius's
Aesop
Aesop's
Afghan
Afghan's
Afghanistan
Afghanistan's
Afghans
Africa
Africa's
African
Africans
Afrikaans
Afrikaans's
Afrikaner
Afrikaner's
Afrikaners
Afro
Afro's
Afrocentrism
Afros
Ag
Ag's
Agamemnon
Agamemnon's
Agassi
Agassi's
Agassiz
Agassiz's
Agatha
Agatha's
Aggie
Aggie's
Aglaia
Aglaia's
Agnes
Agnes's
Agnew
Agnew's
Agni
Agni's
Agra
Agra's
Agricola
Agricola's
Agrippa
Agrippa's
Agrippina
Agrippina's
Aguilar
Aguilar's
Aguinaldo
Aguinaldo's
Aguirre
Aguirre's
Agustin
Agustin's
Ahab
Ahab's
Ahmad
Ahmad's
Ahmadabad
Ahmed
Ahmed's
Ahriman
Ahriman's
Aida
Aida's
Aiken
Aiken's
Aileen
Aileen's
Aimee
Aimee's
Ainu
Ainu's
Airedale
Airedale's
Airedales
Aisha
Aisha's
Ajax
Ajax's
Akbar
Akbar's
Akhmatova
Akhmatova's
Akihito
Akihito's
Akita
Akita's
Akkad
Akkad's
Akron
Akron's
Al
Al's
Ala
Ala's
Alabama
Alabama's
Alabaman
Alabamans
Alabamian
Alabamians
Aladdin
Aladdin's
Alamo
Alamo's
Alamogordo
Alamogordo's
Alan
Alan's
Alana
Alana's
Alar
Alaric
Alaric's
Alaska
Alaska's
Alaskan
Alaskans
Alba
Alba's
Albania
Albania's
Albanian
Albanian's
Albanians
Albany
Albany's
Albee
Albee's
Alberio
Alberio's
Albert
Albert's
Alberta
Alberta's
Alberto
Alberto's
Albigensian
Albion
Albion's
Albireo
Albireo's
Albuquerque
Albuquerque's
Alcatraz
Alcatraz's
Alcestis
Alcestis's
Alcibiades
Alcibiades's
Alcmena
Alcmena's
Alcoa
Alcoa's
Alcott
Alcott's
Alcuin
Alcuin's
Alcyone
Alcyone's
Aldan
Aldan's
Aldebaran
Aldebaran's
Alden
Alden's
Alderamin
Alderamin's
Aldo
Aldo's
Aldrin
Aldrin's
Alec
Alec's
Aleichem
Aleichem's
Alejandra
Alejandra's
Alejandro
Alejandro's
Alembert
Alembert's
Aleppo
Aleppo's
Aleut
Aleut's
Aleutian
Alex
Alex's
Alexander
Alexander's
Alexandra
Alexandra's
Alexandria
Alexandria's
Alexei
Alexei's
Alexis
Alfonso
Alfonso's
Alfonzo
Alfonzo's
Alford
Alford's
Alfred
Alfred's
Alfreda
Alfreda's
Alfredo
Alfredo's
Algenib
Algenib's
Alger
Alger's
Algeria
Algeria's
Algerian
Algerians
Algieba
Algieba's
Algiers
Algiers's
Algol
Algol's
Algonquian
Algonquian's
Algonquians
Algonquin
Algonquin's
Alhambra
Alhambra's
Alhena
Alhena's
Ali
Ali's
Alice
Alice's
Alicia
Alicia's
Alighieri
Alighieri's
Aline
Aline's
Alioth
Alioth's
Alisa
Alisa's
Alisha
Alisha's
Alison
Alison's
Alissa
Alissa's
Alistair
Alistair's
Alkaid
Alkaid's
Allah
Allah's
Allahabad
Allahabad's
Allan
Allan's
Alleghenies
Allegheny
Allegheny's
Allegra
Allegra's
Allen
Allen's
Allende
Allende's
Allentown
Allentown's
Allie
Allie's
Allison
Allison's
Allstate
Allstate's
Allyson
Allyson's
Alma
Alma's
Almach
Almach's
Almaty
Almighty
Almighty's
Almohad
Almohad's
Almoravid
Almoravid's
Alnilam
Alnilam's
Alnitak
Alnitak's
Alonzo
Alonzo's
Alpert
Alpert's
Alphard
Alphard's
Alphecca
Alphecca's
Alpheratz
Alpheratz's
Alphonse
Alphonse's
Alphonso
Alphonso's
Alpine
Alpo
Alpo's
Alps
Alsace
Alsace's
Alsatian
Alsatian's
Alsop
Alsop's
Alston
Alston's
Alta
Alta's
Altai
Altai's
Altaic
Altaic's
Altair
Altair's
Altamira
Altamira's
Althea
Althea's
Altiplano
Altiplano's
Altman
Altman's
Altoids
Altoids's
Alton
Alton's
Aludra
Aludra's
Alva
Alva's
Alvarado
Alvarado's
Alvarez
Alvarez's
Alvaro
Alvaro's
Alvin
Alvin's
Alyce
Alyce's
Alyson
Alyson's
Alyssa
Alyssa's
Alzheimer
Alzheimer's
Am
Am's
Amadeus
Amadeus's
Amado
Amado's
Amalia
Amalia's
Amanda
Amanda's
Amarillo
Amarillo's
Amaru
Amaru's
Amaterasu
Amaterasu's
Amati
Amati's
Amazon
Amazon's
Amazons
Amber
Amber's
Amelia
Amelia's
Amenhotep
Amenhotep's
Amerasian
Amerasians
America
America's
American
Americana
Americana's
Americanism
Americanism's
Americanisms
Americanization
Americanization's
Americanizations
Americanize
Americanized
Americanizes
Americanizing
Americans
Americas
Amerind
Amerind's
Amerindian
Amerindian's
Amerindians
Amerinds
Ameslan
Ameslan's
Amharic
Amharic's
Amherst
Amherst's
Amie
Amie's
Amiga
Amiga's
Amish
Amman
Amman's
Amoco
Amoco's
Amos
Amparo
Amparo's
Ampere
Ampere's
Amritsar
Amritsar's
Amsterdam
Amsterdam's
Amtrak
Amundsen
Amundsen's
Amur
Amur's
Amway
Amway's
Amy
Amy's
Ana
Ana's
Anabaptist
Anabaptist's
Anabel
Anabel's
Anacin
Anacin's
Anacreon
Anacreon's
Anaheim
Anaheim's
Analects
Analects's
Ananias
Ananias's
Anasazi
Anasazi's
Anastasia
Anastasia's
Anatole
Anatole's
Anatolia
Anatolia's
Anatolian
Anaxagoras
Anaxagoras's
Anchorage
Anchorage's
Andalusia
Andalusia's
Andalusian
Andaman
Andean
Andersen
Andersen's
Anderson
Anderson's
Andes
Andorra
Andorra's
Andre
Andre's
Andrea
Andrea's
Andrei
Andrei's
Andres
Andrew
Andrew's
Andrews
Andrianampoinimerina
Andrianampoinimerina's
Andromache
Andromache's
Andromeda
Andromeda's
Andropov
Andy
Andy's
Angara
Angara's
Angel
Angel's
Angela
Angela's
Angelia
Angelia's
Angelica
Angelica's
Angelico
Angelico's
Angelina
Angelina's
Angeline
Angeline's
Angelique
Angelique's
Angelita
Angelita's
Angelo
Angelo's
Angelou
Angevin
Angevin's
Angie
Angie's
Angkor
Angkor's
Anglia
Anglia's
Anglican
Anglicanism
Anglicanism's
Anglicanisms
Anglicans
Anglicize
Anglicized
Anglicizes
Anglicizing
Anglo
Anglo's
Anglophile
Anglophile's
Anglophiles
Anglos
Angola
Angola's
Angolan
Angolans
Angora
Angora's
Angoras
Anguilla
Anguilla's
Angus
Angus's
Aniakchak
Aniakchak's
Anibal
Anibal's
Anita
Anita's
Ankara
Ankara's
Ann
Ann's
Anna
Anna's
Annabel
Annabel's
Annabelle
Annabelle's
Annam
Annam's
Annapolis
Annapolis's
Annapurna
Annapurna's
Anne
Anne's
Annette
Annette's
Annie
Annie's
Annmarie
Annmarie's
Anouilh
Anouilh's
Anselm
Anselm's
Anselmo
Anselmo's
Anshan
Anshan's
Antaeus
Antaeus's
Antananarivo
Antananarivo's
Antarctic
Antarctic's
Antarctica
Antarctica's
Antares
Anthony
Anthony's
Antichrist
Antichrist's
Antichrists
Antietam
Antietam's
Antigone
Antigone's
Antigua
Antigua's
Antilles
Antioch
Antioch's
Antipas
Antipas's
Antofagasta
Antofagasta's
Antoine
Antoine's
Antoinette
Antoinette's
Anton
Anton's
Antone
Antone's
Antonia
Antonia's
Antoninus
Antoninus's
Antonio
Antonio's
Antonius
Antonius's
Antony
Antony's
Antwan
Antwan's
Antwerp
Antwerp's
Anubis
Anubis's
Anzac
Anzac's
Apache
Apache's
Apaches
Apalachicola
Apalachicola's
Apennines
Aphrodite
Aphrodite's
Apia
Apia's
Apocrypha
Apocrypha's
Apollinaire
Apollinaire's
Apollo
Apollo's
Apollonian
Apollos
Appalachia
Appalachia's
Appalachian
Appalachians
Appaloosa
Appaloosa's
Apple
Apple's
Appleseed
Appleseed's
Appleton
Appleton's
Appomattox
Appomattox's
Apr
Apr's
April
April's
Aprils
Apuleius
Apuleius's
Aquafresh
Aquafresh's
Aquarius
Aquarius's
Aquariuses
Aquila
Aquila's
Aquinas
Aquinas's
Aquino
Aquino's
Aquitaine
Aquitaine's
Ar
Ar's
Ara
Ara's
Arab
Arab's
Arabia
Arabia's
Arabian
Arabians
Arabic
Arabic's
Arabs
Araby
Araby's
Araceli
Araceli's
Arafat
Arafat's
Araguaya
Araguaya's
Aral
Aral's
Aramaic
Aramaic's
Aramco
Aramco's
Arapaho
Arapaho's
Ararat
Ararat's
Araucanian
Araucanian's
Arawak
Arawak's
Arawakan
Arawakan's
Arbitron
Arbitron's
Arcadia
Arcadia's
Arcadian
Archean
Archibald
Archibald's
Archie
Archie's
Archimedes
Archimedes's
Arctic
Arctic's
Arcturus
Arcturus's
Arden
Arden's
Arequipa
Arequipa's
Ares
Argentina
Argentina's
Argentine
Argentine's
Argentines
Argentinian
Argentinians
Argo
Argo's
Argonaut
Argonaut's
Argonne
Argonne's
Argos
Argus
Argus's
Ariadne
Ariadne's
Arianism
Arianism's
Ariel
Ariel's
Aries
Arieses
Ariosto
Ariosto's
Aristarchus
Aristarchus's
Aristides
Aristophanes
Aristophanes's
Aristotelian
Aristotle
Aristotle's
Arius
Arius's
Ariz
Ariz's
Arizona
Arizona's
Arizonan
Arizonans
Arizonian
Arizonians
Arjuna
Arjuna's
Ark
Ark's
Arkansan
Arkansan's
Arkansas
Arkansas's
Arkhangelsk
Arkhangelsk's
Arkwright
Arkwright's
Arlene
Arlene's
Arline
Arline's
Arlington
Arlington's
Armageddon
Armageddon's
Armageddons
Armagnac
Armagnac's
Armand
Armand's
Armando
Armando's
Armani
Armani's
Armenia
Armenia's
Armenian
Armenian's
Armenians
Arminius
Arminius's
Armonk
Armonk's
Armour
Armour's
Armstrong
Armstrong's
Arneb
Arneb's
Arnhem
Arnhem's
Arno
Arno's
Arnold
Arnold's
Arnulfo
Arnulfo's
Aron
Aron's
Arrhenius
Arrhenius's
Arron
Arron's
Art
Art's
Artaxerxes
Artaxerxes's
Artemis
Artemis's
Arthur
Arthur's
Arthurian
Artie
Artie's
Arturo
Arturo's
Aruba
Aruba's
Aryan

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.

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

This question has already been answered. Start a new discussion instead.