0

For some reason my if statement is not even being executed at all. Cannot figure out what is wrong, I am trying to test a word called "fuor" and do some manipulations in the hash table I have.

            if(table[getHashIndex(c.toString())].contains(c.toString()))

this is the line that is not executing

Tableclass

char[] c = word.toCharArray();
            for(int i=0;i<word.length()-1;i++)
            {
                char tempChar= c[i];
                c[i]=c[i+1];
                c[i+1]=tempChar;

                if(table[getHashIndex(c.toString())].contains(c.toString()))
                {
                    list.add(c.toString());                 
                   System.out.println("GOT IT BABY");
                }
                c = word.toCharArray();

            }



    public int getHashIndex(String word){
    int key = 7;
    //Adding ASCII values of string
    //To determine the index
    for(int i = 0 ; i < word.length(); i++){
        key = key*BASE+(int)word.charAt(i);
        //Accounting for integer overflow
        if(key<0)
        {
            key*=-1;
        }
    }
    key %= sizeOfTable;
    return key;
}



//Bucket class
public boolean contains(String word){

    Node insert = start;

    //Traversing the list to find a match
    while(insert!=null){
        if(word.equalsIgnoreCase(insert.item))
            return true;

        insert = insert.next;
    }
    //did not find a match
    return false;

}
5
  • 2
    So what exactly do you want us to do? Have you stepped through the code in the debugger? Commented Mar 22, 2014 at 2:36
  • 2
    why don't you break "table[getHashIndex(c.toString())].contains(c.toString())" into several smaller lines of code to see what's happening? I mean, the debugger is the right way to go, but it's no sin to do this when you're learning Commented Mar 22, 2014 at 2:40
  • how is table defined? Commented Mar 22, 2014 at 2:40
  • I found that when I call c.toString on an array it returns some garbage looking values, not sure if its an address or not. This is resulting in a wrong computation of the hash index. Commented Mar 22, 2014 at 2:41
  • It's an object identity string -- not an address but related to it. It's of little use except to identify that object. Commented Mar 22, 2014 at 2:43

1 Answer 1

1

The toString on a Java Array returns the default Object toString() (because arrays are not a primitive type e.g. c.toString() looks like this),

char[] t = new char[] { 'a' };
System.out.println(t.toString());

Output (for example)

[C@25f45022

I think you really wanted something like

char[] t = new char[] { 'a' };
System.out.println(java.util.Arrays.toString(t));

Which outputs

[a]

Or, maybe you wanted something like this

char[] t = new char[] { 'a' };
System.out.println(new String(t));

Which outputs

a
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I just realized that, How do I convert a char array back into a string?
new String(characterArray)

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.