LeatherFace
LeatherFace

Reputation: 476

Code isnt even executing. JAVA

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;

}

Upvotes: 0

Views: 65

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

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

Upvotes: 1

Related Questions