aspose file tools*
The moose likes Beginning Java and the fly likes Hashcode Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashcode Problem" Watch "Hashcode Problem" New topic
Author

Hashcode Problem

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi,

I am tried to complie this pgm,but I am getting errors. Why?

public class Wr
{
public static void main(String s[])
{
String str = new String("Hello World");

Double b2 = Double.valueOf("3.5");

Integer b4 = new Integer("77");

int x = 14;

int obj1 = str.hashcode();
int obj2 = b2.hashcode();
int obj3 = b4.hashcode();
int obj4 = x.hashcode();

System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
}
}



Wr.java:13: cannot resolve symbol
symbol : method hashcode ()
location: class java.lang.String
int obj1 = str.hashcode();
^
Wr.java:14: cannot resolve symbol
symbol : method hashcode ()
location: class java.lang.Double
int obj2 = b2.hashcode();
^
Wr.java:15: cannot resolve symbol
symbol : method hashcode ()
location: class java.lang.Integer
int obj3 = b4.hashcode();
^
3 errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The method is named "hashCode()" -- with a capital "C". Case is significant in Java. You're also going to have problems with this line:

int obj4 = x.hashcode();

because "x" is an int, because you can't call methods on ints or other primitive types.

One more thing, while you're listening: There's essentially never any reason to write

String str = new String("Hello World");

in Java. Strings are immutable so don't need to be copied. Instead, just write

String str = "Hello World";


[Jess in Action][AskingGoodQuestions]
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
Hi Niyas
The 'c' in your hascode() method should be a capital 'C'. Change your code to

Also, an int it a literal and does not have methods so your line

will not work.

Steve
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Yes, Now it is working. Thanks for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashcode Problem