hi im currently learning java and i need to make a game of hangman but i am currenlty stuck making a word list. I want to be able to add words and at the same time check that a word is not duplicated within the list.
someone said try something such as:
sort the list
then check if i = i +1
if they are the same then dont allow the new word
but i do not know how to write this.
i currently have an array or an array list i can use the codes are here the first is the array list
import java.util.ArrayList;
//by dom
// version the one and only
public class Wordbook
{
// Storage for an arbitrary number of words.
private ArrayList words;
//this is to start the word book
public Wordbook()
{
words = new ArrayList();
}
//this is how to add a new word
public void storeWord(String word)//put check instead of store
{
words.add(word);
}
// this shows how many words are in the word book
public int numberOfWords()
{
return words.size();
}
//This is how to show a particular word
public void showWord(int wordNumber)
{
if(wordNumber < 0) {
// This is not a valid word number, so do nothing.
}
else if(wordNumber < numberOfWords()) {
// This is a valid word number, so we can print it.
System.out.println(words.get(wordNumber));
}
else {
// This is not a valid word number, so do nothing.
}
}
}
this is the array
import java.util.*;
/**
* This is the list of words which will be used for the hangman game
*
* @author (dominic)
* @version (the one and only)
*/
public class word_dictionery
{
public static void main (String arguements[])
{
// this is the word list
String [] words = { "cat", "cat"};
// this prints out all the words in the list in alphabetical order
Arrays.sort(words);
System.out.println ( "The word list:" );
for (int i = 0; i < words.length; i++)
System.out.println (i + ": " + words [i] );
//this prints out how many words are in the list
System.out.println ( "there are " + words.length + " words in the word list." ) ;
}
}
if you can help in any way that is great
all the best dom


Reply With Quote


Bookmarks