aspose file tools*
The moose likes Beginning Java and the fly likes calling a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling a method" Watch "calling a method" New topic
Author

calling a method

Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
I'm trying to call the calcHash method to get a hash index but I am getting the cannot resolve symbol error message(not recognising calcHash).I am a bit stumped.


class Student
{
String name;
String course;
int hashIndex;
public Student(String name,String course)
{

this.name =name;
this.course =course;

}

public String getName()

{

return name;
}

public String getCourse()

{
return course;

}

public int calcHash(int maxNum)

{
int hashTotal = 0;
int hashIndex = 0;
for(int i = 0; i<name.length();i++)
{

char letter = name.charAt(i);
int asciiValue = letter;
hashTotal = hashTotal + asciiValue;
hashIndex = hashTotal % maxNum;
}

return hashIndex;
}
}
class Storage
{

int maxNum = 4;
Storage StArray[] = new Storage[4];
public void add(int hashIndex,String name)
{
for(hashIndex=0;hashIndex<maxNum;hashIndex++)
StArray[hashIndex]=calcHash(maxNum);
}
}
Shalini Chandel
Ranch Hand

Joined: May 05, 2005
Posts: 115
Hey Alan,

You are trying to access a member method calcHash() of class Student from Storage class.
And as per OOPS, You cannot access any member of the class without creating an object.
So create an object of Student class in Storage class and use the object to call the method.
e.g.



Regards,
Shalini


SCJP 1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Shalini Chandel:



This won't work... the Student class doesn't have a default constructor.

Henry


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

Joined: Oct 27, 2004
Posts: 198
Deja Vu. This thread seems to be an exactly like another one just recently. Same classes, methods and problem. Maybe I've been reading this board too much, or not getting enough sleep.

Edit: Okay I'm not going crazy (what a relief). Alan asked this exact question a week ago.
http://www.coderanch.com/t/399650/java/java/Calling-method

Alan, you could have just used the old thread and kept going. Did none of the suggestions in that thread help?

[ May 19, 2005: Message edited by: Hentay Duke ]

Edit2: One more thing Alan, a week later and you still haven't figured out the code tags when posting code? Come on man they're not that difficult!
[ May 19, 2005: Message edited by: Hentay Duke ]
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
You've got a few problems here. Check the comments in this code.
Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
Thanks Hentay.Sorry If I'm repeating myself,and I can't deny I'm having some problems with this,but please remember that maybe even you were a novice once.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
No problem at all. Did my code make sense to you? What problems are you still having if any?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling a method