Can anyone tell me what is wrong with this code for calling a method from one class which is in another method and class.
The following code is in a method called StoreStudent which is in a Class called Storage.
The method I am calling i.e. HashIndex( ) is in a class called Student.
When I run the program I get the following error message: C:\java>javac Storage.java Storage.java:102: cannot resolve symbol symbol : method HashIndex () location: class java.lang.String int Hash = Student.HashIndex( ) ;
I don't understand when I have stated that I want to call Student.HashIndex( )
Please can anyone help or point me into a place where I can investigate further.
The error message seems to indicate that there's a variable "Student" of type "String" (array of String) and that's what the compiler is calling the method on. To call the method you're interested in, you need an actual instance of class Student -- an object created using "new Student" (unless this is a static method.)
This is one of many reasons why it pays to follow the standard Java convention of naming ClassesLikeThis and variablesLikeThis -- i.e., upper case names for classes, and lower-case names for variables and methods. Makes understanding code at a glace easier.
I have now ensured my code conforms. Thanks for noting this. I was unaware of this.
My code is now as follows:
I have a class called Student. Within the Student class I have a method called hashIndex.
I have a class called Storage. Within the Storage class I have a method called StoreStudent.
The error message I get is: C:\java>javac Storage.java Storage.java:105: ']' expected newStudent studentDetail [hash] = Student.studentName; //Put the Student object in the array as HashIndex Why, Oh Why - is this occuring? Many times I have stored things in arrays. Is it to do with the fact that I am calling a method from one class to a method in another class? But if it is, surely the line of code to put the Student object in the array as HashIndex is within the same class!
The problem is that the compiler thinks you are trying to declare an array here, but this is the wrong syntax. Specifically, you are not allowed to have anything between [ and ] in the declaration.
By looking at your other code, I think you are trying to assign an element to an array that should have been declared somewhere else. There are a couple of places you need to make changes in order to fix this. Looking at your constructor, I see another array declaration:
However, this array is not the same as the array you declared later. In order to share this array variable, you should declare it as a class member, i.e. outside of any method. In otherwords, you can change your code to look something like this:
Notice that I changed the variable declarations to private. This is a common practice because you usually don't want to allow other classes to arbitrarily change these values; they should only be set in a controlled way by calling methods on objects of this class.
Also notice that I eliminated the keyword "static" on the declarations. This allows you to create multiple Storage objects that each have their own array of students. Keeping the "static" keyword means that the variable is shared by all instances of the class.
Finally, there are still problems with the storeStudent() method. However, I don't know exactly how to help you fix them without seeing your Student class. Besides, I'd like you to make the above changes to your code so you can see next set of compiler errors. Figuring out what these errors mean is a valuable skill.
I hope this helps. Please let us know if you have any more problems.
Sorry for the delay in posting this thread (as suggested in my email). I had difficulty getting on to JavaRanch Bigg Moose Saloon.
As my program was rather long to post here, I emailed it to you.
The error message I am now getting is detailed below: ERROR MESSAGE: C:\java>javac StorageTest.java StorageTest.java:128: hashIndex(java.lang.String,int) in Student cannot be appli ed to () hash = Student.hashIndex( );
^ StorageTest.java:130: upperCase(java.lang.String) in Student cannot be applied t o () students [hash] = Student.upperCase( ); ^ 2 errors
My understanding is I'm getting this error message because the variable are not of the same type? If this is the case the method hashIndex in the Student file i.e. Student.hashIndex returns an int variable. So I guess this can't be the case?
Please forgive me if I am looking an idiot, this is the first time I am using methods declaired in other classes and I am experiencing difficulty with getting my head around it all.
Eventually I want to store each class in it's own file and call it from differnt files. Do you know where I can find examples of this as all the examples I have are contained within one file. The methods used are all in the same class too.
The several error messages you show in a previous post are two different instances of the same thing: you've written a function that expects arguments, and then you're trying to call it without passing any. A valid call to (for example) your Student.hashIndex method would have to look like