0

I'm attempting to return the index of where an object appears in an array of objects.

public static int search(WordCount[] list,WordCount word, int n)
{
    int result = -1;
    int i=0;
    while (result < 0 && i < n)
    {
        if (word.equals(list[i]))
        {
            result = i;
            break;
        }
        i++;
    }
    return result;
}

WordCount[] is the array of objects.

word is an instance of WordCount.

n is the number of objects in WordCount[]

It runs, but isn't returning the index correctly. Any and all help is appreciated. Thanks for your time.

CLASS

class WordCount
{
String word;
int count;
static boolean compareByWord;
public WordCount(String aWord)
{
    setWord(aWord);
    count = 1;
}
private void setWord(String theWord)
{
    word=theWord;
}
public void increment()
{
    count=+1;
}
public static void sortByWord()
{
    compareByWord = true;
}
public static void sortByCount()
{
    compareByWord = false;
}
public String toString()
{
    String result = String.format("%s (%d)",word, count);
    return result;
}
}

How I'm calling it...

for (int i=0;i<tokens.length;i++)
        {
            if (tokens[i].length()>0)
            {
                WordCount word = new WordCount(tokens[i]);
                int foundAt = search(wordList, word, n);
                if (foundAt >= 0)
                {
                    wordList[foundAt].increment();
                }
                else
                {
                    wordList[n]=word;
                    n++;
                }
            }
        }
    }
10
  • 2
    What is it returning? Commented Apr 15, 2014 at 23:29
  • 2
    Show us your equals implementation for WordCount. Commented Apr 15, 2014 at 23:32
  • Have you overridden the equals() and hashCode() methods in WordCount? Commented Apr 15, 2014 at 23:33
  • It is supposed to return the index of where word is found in WordCount. Commented Apr 15, 2014 at 23:35
  • 2
    According to the comment, you have created your own class WordCount, but you have not overridden the equals method. Post your WordCount class, and you'll receive help quickly. Commented Apr 15, 2014 at 23:38

4 Answers 4

1

By default, Object#equals just returns whether or not the two references refer to the same object (same as the == operator). Looking at what you are doing, what you need to do is create a method in your WordCount to return word, e.g.:

public String getWord() {
    return word;
}

Then change your comparison in search from:

if (word.equals(list[i]))

to:

if (word.getWord().equals(list[i].getWord()))

Or change the signature of the method to accept a String so you don't create a new object if you don't have to.

I wouldn't recommend overriding equals in WordCount so that it uses only word to determine object equality because you have other fields. (For example, one would also expect that two counters were equal only if their counts were the same.)

The other way you can do this is to use a Map which is an associative container. An example is like this:

public static Map<String, WordCount> getCounts(String[] tokens) {
    Map<String, WordCount> map = new TreeMap<String, WordCount>();

    for(String t : tokens) {
        WordCount count = map.get(t);
        if(count == null) {
            count = new WordCount(t);
            map.put(t, count);
        }

        count.increment();
    }

    return map;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks man I had no idea it was as simple as throwing a getWord function into my class. Been stewing over this issue for the last 3 hours. For some reason my increment isn't working though? When I print out the output it is only printing a (1) for all the words. Any ideas where I'm going wrong here?
I'm an idiot, found my problem. I really appreciate all the help
0

This method is probably not working because the implementation of .equals() you are using is not correctly checking if the two objects are equal.

You need to either override the equals() and hashCode() methods for your WordCount object, or have it return something you want to compare, i.e:word.getWord().equals(list[i].getWord())

Comments

0

It seems easier to use:

public static int search(WordCount[] list, WordCount word)
{
    for(int i = 0; i < list.length; i++){
        if(list[i] == word){
            return i;
        }
    }
    return -1;
}

This checks each value in the array and compares it against the word that you specified.

1 Comment

This won't work, WordCount is not comparable using the == operator
0

The odd thing in the current approach is that you have to create a new WordCount object in order to look for the count of a particular word. You could add a method like

public boolean hasEqualWord(WordCount other)
{
    return word.equals(other.word);
}

in your WordCount class, and use it instead of the equals method:

....
while (result < 0 && i < n)
{
    if (word.hasEqualWord(list[i])) // <--- Use it here!
    { 
    ....
    }
}

But I'd recommend you to rethink what you are going to model there - and how. While it is not technically "wrong" to create a class that summarizes a word and its "count", there may be more elgant solutions. For example, when this is only about counting words, you could consider a map:

Map<String, Integer> counts = new LinkedHashMap<String, Integer>();
for (int i=0;i<tokens.length;i++)
{
    if (tokens[i].length()>0)
    {
        Integer count = counts.get(tokens[i]);
        if (count == null)
        {
            count = 0;
        }
        counts.put(tokens[i], count+1);
    }
}

Afterwards, you can look up the number of occurrences of each word in this map:

String word = "SomeWord";
Integer count = counts.get(word); 
System.out.println(word+" occurred "+count+" times);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.