• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

calling a method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23923
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 Shalini Chandel:



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

Henry
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem at all. Did my code make sense to you? What problems are you still having if any?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic