• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Hashcode program error

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I used parentheses but still getting the same error.Please Help.Thanks
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

}
}
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. In what way do I need to change the code so as to get the output as

suzuki
yamaha
TVS.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok. I did not see that. Thanks
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, just being anal here... you could have also coded the equals() method like so:



Henry
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi henry, I have properly overridden the hashcode method.Please check the code which I have given above.Thanks
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic