wood burning stoves 2.0*
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
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
Hi
I am trying to call a method from a Student class that calculates a hash code for a Storage class.I am getting a cannot resolve symbol error in the Storage class and can see nothing wrong with the line.It's probably something minor but I need another set of eyes.
Cheers.
The code:

public 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;
}
}
public class Storage
{

int maxNum = 4;
Storage Store = new Storage();
Storage StArray[] = new Storage[4];
public void getHash(String name[])
{
for(int hashIndex=0;hashIndex<maxNum;hashIndex++)
StArray[hashIndex]=Store.calcHash(maxNum);

}
}
I am getting the error;:
Storage.java:10:cannot resolve symbol
symbol :method calcHash (int)
location:class Storage
StArray[hashIndex]=Store.calcHash(maxNum);
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

The Storage class does not have a calcHash() method.

Henry


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

Joined: Jul 30, 2003
Posts: 804
I see a few things:

- The method calchash appears to be a method of class Student, yet you're invoking it as though it were a method of class Storage.

- I recommend using lower case names for objects because seeing an upper case name makes me think "oh, that's a class, so it must be a static method call", when in fact it's not. This is standard coding convention in Java.

- I see no main method to run this from.

- Please please use UBB CODE tags to enclose your code excerpts. Doing so preserves the formatting, making the code much easier to read.

Hope that helps. Good luck!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
Sorry here is the main method.

import javax.swing.JOptionPane;
public class MainProg
{
public static void main(String[] args)
{

String[] name = new String[4];
String[] course = new String[4];
int lp = 0;
int maxNum = 4;
System.out.println("Name\t\t\tCourse");

for(lp=0;lp<name.length;lp++)
{

name[lp] = JOptionPane.showInputDialog("Enter name");
}

for(lp=0;lp<course.length;lp++)
{

course[lp] = JOptionPane.showInputDialog("Enter course");

}
for(lp=0;lp<4;lp++)
{
System.out.println(name[lp]+"\t\t"+course[lp]);

}

System.exit(0);
}


}
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
I think he meant you should actually put your code inside the code tags. But anyway, the problem is not in the main method it's as has been described. The calcHash method is in Student, but you calling it as if it's in Storage. So when it looks for calcHash in Storage it can't find it and gives you the error you see.
Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
Yes I see that now....sorry about the tags!!
Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
Tried using a Student object instead.Doesn't seem to work
Error is:
Student(java.lang.String.jav.lang.String)in Student cannot be applied to()
Student stud = new Student();
^
class Storage
{

int maxNum = 4;
Student stud = new Student();
Storage StArray[] = new Storage[4];
public void getHash(String name[])
{

for(int hashIndex=0;hashIndex<maxNum;hashIndex++)
StArray[hashIndex]=stud.calcHash(maxNum);

}
}
Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
I'll sort the tabs out eventually by the way
Alan
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

Student(java.lang.String.jav.lang.String)in Student cannot be applied to()
Student stud = new Student();


Take a look at your own source code... You designed student to take a name and a course.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a method
 
Similar Threads
Arrays: Calling input from other methods and classes
hashing problem
Hashing Problem
calling a method
Hashing Storage