Solved

Adding words to a setADT

Posted on 2004-10-21
255 Views
Last Modified: 2012-06-21
trying to write an application which used setADT as a spelling checker

how do you add words into the set?
then i want to read in some text and check to see which words are not in the 'known words' list.

all of my source code is located at

http://www.newwavesound.co.uk/java

even if some of the theory behind this is explained i mite be able to manage the coding myself.

any help is appreciated.

thanks pete




0
Question by:pete420
    14 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    What about something like:

    import java.util.*;

    public class WordSet implements SetADT {
          Set words;
          
          public WordSet() {
                words = new HashSet();
          }
          
          public add(Object o) {
                words.add(o);
          }
    }
    0
     

    Author Comment

    by:pete420
    while im sure that would work, i have not learnt hash tables yet so couldn't use one.

    ta anyways

    pete
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>yet so couldn't use one.

    You need a Set. That's not a hash table. The only alternative would be to write your own - why give yourself such a difficult task?
    0
     

    Author Comment

    by:pete420
    sorry, i should have wrote at the top that this was for a uni,

    i cant use things i havent learnt yet for that reason,

    at the minute i just hard coded the words in like:

                String word1 = "test";
                Words.add (word1);
                String word2 = "that";
                Words.add (word2);            
                String word3 = "this";
                Words.add (word3);            
                String word4 = "is";
                Words.add (word4);            
                String word5 = "working";
                Words.add (word5);

    not the best way to do it im sure but it is inputing the words into the set,


    ta pete
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    What collection are you going to use to hold the words?
    0
     

    Author Comment

    by:pete420
    im using a Set ADT using arrays, i think thats what you want to know


    my source is available at:
    http://www.newwavesound.co.uk/java


    if i havent answered ur question,


    pete
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    OK, although in Java that is not a Set, as a Set cannot included duplicates. Where are we now - i'm not sure where you need help?
    0
     

    Author Comment

    by:pete420
    is what i have done so far not a set????

    how do i make it no duplicates?

    pete
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>is what i have done so far not a set????

    No, not in Java terms, as your ArraySey.add does not exclude duplicates. You'd have to check for dups before adding
    0
     

    Author Comment

    by:pete420
    so in add, i should say something like:

    if word already exists throw and error or exception telling the user the word is already in the set


    pete
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Not necessarily an error. You could even replace the old one by the new one, but here's one way of approaching it:


                public void add(Object element) {
                      boolean alreadyAdded = false;
                      for (int i = 0; (i < contents.length) && (alreadyAdded == false); i++) {
                            if (contents[i].equals(element)) {
                                  alreadyAdded = true;
                            }
                      }
                      if (alreadyAdded == false) {
                            if (size() == contents.length) {
                                  expandCapacity();
                            }
                            contents[count] = element;
                            count++;
                      }
                }
    0
     
    LVL 92

    Accepted Solution

    by:
    your add() should look like:

       public void add (Object element)
       {
           if (!contains(element))
           {
               if (size() == contents.length)
          expandCapacity();
                
               contents[count] = element;
               count++;
           }
       }

    0
     

    Author Comment

    by:pete420
    cheers, i will try it out..


    it appears im lookin for answers i cant get here. I will have to start from the start and learn the whole thing from scratch.

    the help was appreciated tho,. will split the points cuz u's were decent enough to reply :)

    pete
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>,. will split the points

    So what happened to the split!?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 50
    java constructor error 8 45
    Jasper Report: Crosstab Report- Include Page Footer 2 22
    countX 22 31
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now