• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling a method

 
Alan Arkwell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shalini Chandel:



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

Henry
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got a few problems here. Check the comments in this code.
 
Alan Arkwell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem at all. Did my code make sense to you? What problems are you still having if any?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic