DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2005
    Posts
    3

    ARRAY OR ARRAYlIST JAVA PROBLEM

    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

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you want to store unique words then use a plain ArrayList. This class has a
    boolean method; contains(Object ob) so if you make sure that the words you put
    into it are all lowercased (or....) you can do like this
    Code:
    String newWord=getThatWord(); 
    newWord=newWord.toLowerCase();
    if (!theList.contains(newWord)) {
      theList.add(newWord);
    }
    The reason for this is that the ArrayList class uses its contained elements' equals
    method when processing the contains(..) method. So you don't have to sort for this.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links