Okay so I am currently workng on an assignment where I have basically got to create a word ladder, eg. Click, Clock, Flock, Flick, so its a list of words with only 1 letter difference, and the same letters cant be reused. Now I can do 2 methods of doing this, the first (which I need help need help with right now) is generate, which the user inputs a word along with the number of steps, and then the program generates a word ladder for that word with the number of steps given.
Now there is another method but since Im not working on that right now, I am focusing on the generate and I need help with it. Here's the main code for the one class so far.
Code :
package uk.ac.aber.dcs.cs21120.wordplay.data;
import java.util.*;
import java.io.*;
public class WordLadder {
private String word;
private ArrayList<String> words = new ArrayList<String>();
private String fileName = "";
private BufferedReader buf;
private int number;
private Hashtable<String,String> hashWords;
//private char letter;
public WordLadder() {
}
public void initialize() {
Scanner scan1 = new Scanner(System.in);
//Ask user for length of word input.
System.out.println("How many characters does the word contain?\n" +
"Enter a value between 2 and 7 inclusive.");
String wordLength = scan1.nextLine();
fileName = "dict" + wordLength + ".dat";
readFromFile(fileName);
}
public void readFromFile(String fileName) {
try {
//Construct the BufferedReader object
buf = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = buf.readLine()) != null) {
//Process the data, here we just print it out
words.add(line);
}
} catch (FileNotFoundException ex) {
System.out.println("Sorry this file could not be found.");
ex.printStackTrace();
this.initialize();
} catch (IOException ex) {
ex.printStackTrace();
this.initialize();
} finally {
//Close the BufferedReader
try {
if (buf != null)
buf.close();
} catch (IOException ex) {
ex.printStackTrace();
this.initialize();
}
// THIS IS NEW - LOOK AT THIS AND UNDERSTAND WHAT IT IS DOING - THEN DELETE THIS COMMENT
// create the HashTable with the same capacity as the number of words in the ArrayList (Dictionary)
hashWords = new Hashtable<String,String>(words.size());
// loop through the ArrayList and place each word into the Hashtable
for (String w: words)
{
hashWords.put(w,w);
}
}
}
public void generate() throws IOException {
//get word from user
int stepNumber=0;
Scanner scanGen = new Scanner(System.in);
//Ask user for word input.
System.out.println("What word would you like to use?");
String userWord=scanGen.nextLine();
//READ THIS THEN DELETE THIS COMMENT - THIS NOW CHECKS THAT THE WORD IS IN THE HASHTABLE
//Must check that the word is in the dictionary.
if(!(validate(userWord))) {
System.out.println("Sorry, this word is not in my dictionary. Please try again.");
return;
}
//Ask user for step number input.
System.out.println("How many Numbers do you want generated?");
// DELETE THIS COMMENT AND THE NEXT LINE - PROGRAM IS EXPECTING 2 NUMBERS
try {
stepNumber=scanGen.nextInt(); // get the user entry
scanGen.nextLine(); // advance the Scanner onto the next line
// Print this to screen for test purposes
System.out.println("The word is "+userWord+" and the number of steps required is "+stepNumber);
}
catch(NumberFormatException ex) {
System.out.println("I could not understand. Re-enter your value.");
stepNumber=scanGen.nextInt();
}
System.out.println("Thank you");
// NOW YOU HAVE THE START WORD AND THE NUMBER OF STEPS,
// CALL THE searchForNewWord() passing in the userWord and stepNumber
// This method can do all the work looking for words to pass back a String
String ladder = searchForNewWord(userWord, stepNumber);
// Now as long as the String is not null, you can print it to screen
}
// THIS METHOD NOW CHECKS THE HASHTABLE INSTEAD OF THE ARRAYLIST
public boolean validate(String wordToCheck) {
if(hashWords.contains(wordToCheck)) {
return true;
}
else {
return false;
}
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
/**
* This method is used to search for a new word while removing the word that was currently used to search for this new word.
* @param word - This is the word that is entered by the user.
* @return
*/
public String searchForNewWord(String word, int steps) {
//string below is used for a test
System.out.print("The words in the ladder include: ");
// your userWord is passed in as a parameter
String userWord = word;
int numWordsToFind = steps;
// You need to find all words the amount of steps
// so a loop
for (int i=0; i<numWordsToFind; i++){
// THE ITERATOR BELONGS TO THE HASHTABLE
Iterator wIt = hashWords.iterator();
while(wIt.hasNext()) {
Object startWord = wIt.next();
System.out.print(toString() + " ");
}
for(i=wordLength.userWord) {
charAt(i).startWord;
}
}
}
}
/**
* This runMenu() method runs the many options that are linked to the printMenu() method.
* @throws IOException - This Input Output Exception is used in case any input that isn't shown in the menu commands is entered.
*/
public void runMenu()throws IOException{
String response="";
Scanner scanMenu =new Scanner(System.in);
do {
printMenu();
response=scanMenu.next();
if (response.equals("1")|| response.equals("Generate")|| response.equals("exit")) {
generate();
}
else if (response.equals("2")|| response.equals("Discover")|| response.equals("discover")) {
return;
//discover();
}
else if (response.equals("3")|| response.equals("Re-Enter")|| response.equals("re-enter")|| response.equals("Reenter")|| response.equals("reenter")) {
initialize();
}
else if (response.equals("Q")|| response.equals("q")||
response.equals("Quit")|| response.equals("quit")) {
System.out.println("Thanks you for using Word Play, I hope you enjoyed your time!");
}
else {
System.out.println("Sorry, didn't quite get that. Could you repeat yourself?");
}
} while (! ( (response.equals("Q"))|| (response.equals("q"))));
}
/**
* printMenu() method runs from the menu and shows all commands for the runMenu() method.
* This shows what the main menu will looks like when it is run from the 'WordPlay' class.
*/
private void printMenu() {
System.out.println("What would you like to do?");
System.out.println("1 - Generate");
System.out.println("2 - Discover");
System.out.println("3 - Re-enter character length");
System.out.println("Q - Quit the game");
}
}
The main problem I am having with here is the Generate() and more precisely, the searchForNewWord method. The generate is fine to the point of needing the searchForNewWord, and this method needs to search for a new word once the user has first inputted the starting word for the word ladder. Now I am trying to use an iterator along with the already created hashtable but I am having trouble as to how to use this to its fully capacity. If someone can help explain how to get what I wish to do in a simple manner that would be fantastic.