aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hashcode program error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hashcode program error" Watch "Hashcode program error" New topic
Author

Hashcode program error

Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
import java.io.*;
import java.util.*;
import java.lang.*;
class abc
{
String s;
public abc(String s1)
{
s=s1;
}
public String toString()
{
return s;
}
public static void main(String[]args)
{
Map m1=new HashMap();
m1.put("Yamaha",new abc("suzuki"));
System.out.println(m1.get("Yamaha"));
m1.put(new Integer(20),new abc("honda"));
System.out.println(m1.get(new Integer(20)));
m1.put(new bcd(new Integer(10)),new abc("TVS"));
System.out.println(m1.get(new bcd(new Integer(10))));

}}

class bcd
{
Integer i;
public bcd(Integer j)
{
i=j;
}
public int hashcode()
{
return 9;
}
public boolean equals(Object o)
{
if((o instanceof bcd)&&(((bcd)o.i)==i))//line 1
{
return true;
}
return false;

}
}



I am getting compiler error at line 1. It says variable 'i' cannot resolve symbol and incomparable types.Please help me out.Thanks


SCJP 5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
Please UseCodeTags when posting code of any length. It is unnecessarily hard to read as it is.

The "o.i" field access has a higher precedence than the "(bcd)" cast. In other words, you need to add a pair of parentheses.
[ January 02, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Hi, I used parentheses but still getting the same error.Please Help.Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Originally posted by Thirumalai Muthu:
Hi, I used parentheses but still getting the same error.Please Help.Thanks


Well, this depends on where you put those parentheses. Care to show us the new code?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Charmy Madhvani
Ranch Hand

Joined: Dec 26, 2007
Posts: 59
import java.io.*;
import java.util.*;
import java.lang.*;
class abc
{
String s;
public abc(String s1)
{
s=s1;
}
public String toString()
{
return s;
}
public static void main(String[]args)
{
Map m1=new HashMap();
m1.put("Yamaha",new abc("suzuki"));
System.out.println(m1.get("Yamaha"));
m1.put(new Integer(20),new abc("honda"));
System.out.println(m1.get(new Integer(20)));
m1.put(new bcd(new Integer(10)),new abc("TVS"));
System.out.println(m1.get(new bcd(new Integer(10))));

}}

class bcd
{
Integer i;
public bcd(Integer j)
{
i=j;
}
public int hashcode()
{
return 9;
}
public boolean equals(Object o)
{
if((o instanceof bcd)&&((((bcd)o).i)==i))//line 1(Changed here)
{
return true;
}
return false;

}
}


SCJP 5.0<br />SCWCD 5.0
Charmy Madhvani
Ranch Hand

Joined: Dec 26, 2007
Posts: 59
import java.io.*;
import java.util.*;
import java.lang.*;
class abc
{
String s;
public abc(String s1)
{
s=s1;
}
public String toString()
{
return s;
}
public static void main(String[]args)
{
Map m1=new HashMap();
m1.put("Yamaha",new abc("suzuki"));
System.out.println(m1.get("Yamaha"));
m1.put(new Integer(20),new abc("honda"));
System.out.println(m1.get(new Integer(20)));
m1.put(new bcd(new Integer(10)),new abc("TVS"));
System.out.println(m1.get(new bcd(new Integer(10))));

}}

class bcd
{
Integer i;
public bcd(Integer j)
{
i=j;
}
public int hashcode()
{
return 9;
}
public boolean equals(Object o)
{
if((o instanceof bcd)&&((((bcd)o).i)==i))//line 1(Changed here)
{
return true;
}
return false;

}
}

This code compiles and gives the output
suzuki
honda
null
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Hi Sorry Henry for not posting the code.Thanks Charmy I got it but Should not the output be

suzuki
honda
TVS

instead of suzuki
honda
null As I have overridden the hashcode and equals methods in class bcd.Please clarify on this.Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Originally posted by Thirumalai Muthu:
Hi Sorry Henry for not posting the code.Thanks Charmy I got it but Should not the output be

suzuki
honda
TVS

instead of suzuki
honda
null

As I have overridden the hashcode and equals methods in class bcd.Please clarify on this.Thanks


You overridden your equals() method to compare references. And since you created different Integer(10) references, those objects won't compare -- hence, null is returned.

Henry
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75


In the above code I have used Strings as keys and I have got the output as

suzuki
yamaha
TVS

but when the code is being changed to like this(line 1 and 2)


The output is

suzuki
yamaha
null

I really don't understand why I am getting null. Sorry Henry I was not able to understand your explanation. I would appreciate if you explain it in detail.Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

In the above code I have used Strings as keys and I have got the output as

suzuki
yamaha
TVS

but when the code is being changed to like this(line 1 and 2)


The equals() method of the String class compares the value of the strings themselves for equality. You class does not. You class compares the reference of the internal strings for equality.

I really don't understand why I am getting null. Sorry Henry I was not able to understand your explanation. I would appreciate if you explain it in detail.Thank


I don't know what to explain -- as there isn't anything more left to explain. Except, maybe do it for you....



Henry
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Ok. In what way do I need to change the code so as to get the output as

suzuki
yamaha
TVS.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Originally posted by Thirumalai Muthu:
Ok. In what way do I need to change the code so as to get the output as

suzuki
yamaha
TVS.


Replace the equals() method of your bcd class with the code that I provided in the previous post.

Henry
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Oh ok. I did not see that. Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

BTW, just being anal here... you could have also coded the equals() method like so:



Henry
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Hi Henry, I tried both of your substitution codes but still I am getting the same old output

suzuki
honda
null

Can you please check it out.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Originally posted by Thirumalai Muthu:
Hi Henry, I tried both of your substitution codes but still I am getting the same old output


You also never overrode the hashCode() method. The default hashcode will try to throw different objects into different buckets.

Henry
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Hi henry, I have properly overridden the hashcode method.Please check the code which I have given above.Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Originally posted by Thirumalai Muthu:
Hi henry, I have properly overridden the hashcode method.Please check the code which I have given above.Thanks


Please change your hashcode() method to hashCode() method... notice the capital C. BTW, in the future, override your methods like so:



The Override tag will tell your compiler that you are attempting an override of a method, and will warn you if no such override exists (in this case, due to a spelling error).

Henry
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
Hi Henry ,Thank you very much for spending your valuable time in giving me prompt replies and solving the problem.Now I am getting the correct output Thank you so much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hashcode program error