File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Arrays:  Calling input from other methods and classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays:  Calling input from other methods and classes" Watch "Arrays:  Calling input from other methods and classes" New topic
Author

Arrays: Calling input from other methods and classes

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have a line of code within a method called storeStudent in a class called storage.


The error message I am getting is:

C:\java>javac StorageTest.java
StorageTest.java:130: incompatible types
found : java.lang.String
required: Student
students [hash] = Student.upperCase(Student.studentName);
^
The line of code in the method called storeStudent in a class called storage is:


The variables being passed from the public Main Program is:


Firstly, I don't understand what the above error message is saying. Can anyone elaborate on this and point me in the right direction please?

Secondly, Is it okay to have a main program with various methods then have a number of classes that calls the input from different classes? I have assumed it is. But don't understand why this is not working. I have another line of code where I have successfully managed to do this but not in an array:


My aim is to store the name in the array in the hash position i.e. students [hash] = student name.

(All help and suggestions is appreciated)
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi Maureen,

The error message is telling you that you are trying to assign the output of a method that returns a String object, i.e.

The

Returns a String, presumably the name of the student in uppercase. However, you are trying to assign this to an array, students, that expects an object of the Student type.

You need to fix this along the lines:


Thus you put the whole Student object, names, courses and all, into your array.

Does this help?

DD


SCJP 1.4<br />SCWCD (in progress)
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Mmmmmm - I think you have hit a note here on something I am not understanding correctly.........

You mention defining a constructor for my student class that takes name and course strings. I have a method in my student class that takes the name and course strings. When I put them in a constructor I was advised not too as a new instance was not instanciated. I also thought that a constructor was used only to initialise something. Have I missed something here?
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
You have been stuck on this for days now, so here is some example code to help you along.
Please note that this is only an example. You will have to write the rest of it yourself. (Hope this displays OK).


DD
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Dun Dagda, many thanks for your reply. I will digest this.

I have been on this problem for months (since the beginning of August). Getting to the point when I think maybe me and java don't get along. Had many a sleepless night. Bought loads, and loads of books. Just trying to take name and course from user, put this into a hash index and then print it out. Gone into dynamic array (as nothing stated how many inputs the user would give) then went into hashmaps - managed these two successfully in the end. Absolute nightmare! Just feel I'm going round, and round in circles.

Guess I will get there in the end. Thanks for your help. No doubt I will be asking more questions.

Thanks again!
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi Maureen,

Glad I could be of some help. Don't despair, I used to think the same about Java, thought I would never get it. But once I cracked this problem things got much easier. Are you using the online NETg material much? I found it quite helpful. Also, talk to your tutors - they are friendly guys and willing to help.

Keep it up! You can do it!!

DD
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hi - I'm back again with a few questions on your sample code above.

In the Student class and again in the Storage constructor I notice you have used the variable numStudents. Am I assuming correctly that this hasn't been initialised anywhere because it is sample code?

In the Storage class and the storeStudent method you are passing a parameter called Student studentObject. Could you elaborate on this for me? If studentObject is a variable this has also not been declared anywhere. Again, is this because it is sample code?

I also notice you have used the following:

/**
*@param args the command line arguments
*/

I am assuming this is comments only?

In the public class TestStudent at the end, the first line in the for statement you use:
Student output = store.retrieveStudent(i);

I understand that retrieveStudent is a method in the Storage class, was store meant to be Storage then?

Thanking you again
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

Hi - I'm back again with a few questions on your sample code above.
In the Student class and again in the Storage constructor I notice you have used the variable numStudents. Am I assuming correctly that this hasn't been initialised anywhere because it is sample code?


You will notice that numStudents is a class variable in Storage and is indeed initialised to the value 0. When the constructor of that class is called in TestStudent the value is changed to 3.
In class Student numStudents is used as the name for the variable passed into the hashCode method, as in this case it is a local variable.



In the Storage class and the storeStudent method you are passing a parameter called Student studentObject. Could you elaborate on this for me? If studentObject is a variable this has also not been declared anywhere. Again, is this because it is sample code?

The Student instances are initialised in the TestStudent class, it is in this class that these instances are then passed to the Storage class.



I also notice you have used the following:

/**
*@param args the command line arguments
*/

I am assuming this is comments only?

Yes this is just a comment.


In the public class TestStudent at the end, the first line in the for statement you use:
Student output = store.retrieveStudent(i);

I understand that retrieveStudent is a method in the Storage class, was store meant to be Storage then?

Thanking you again

store is an instance of the class Storage and is created in the TestStudent class.

HTH
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Dear Nigel,

I had to leave the forum for a while to get on with some other work, so thank you very much for responding to Maureen's questions for me.

DD
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
By the way, Maureen, can you get the code I posted to compile and run OK on your system? It worked fine on my system, so it should be alright for you (I hope!).

DD
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Dun,

Good to read your posting. Thanks for staying with me on this.

I ran your code and Yes, no problems at all.

I'm still trying to get my head around everything though. Trying a few things out. Will no doubt get back to ask you some more questions when I've tried things myself a bit more.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hi, well I'm back asking more questions.

The program compiles ok. But I now get the following error message.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Storage.storeStudent(StorageTest.java:127)
at StorageTest.main(StorageTest.java:182)

I only put one name and course in and get the above error message. My array size has been set to 10 (which concerns me as I have no idea what the array size should be as the user could enter 50 or a million, but I will face that one again later. I assume I will have to use a dynamic array? Am I thinking along the right path? As this is where I started three months ago )

My main program is like this:


The Student class works fine however the Storage class is a different story.

In the Student class I have the following code:


In the Storage class I have the following:


I think where the error is occuring is on the line students [hash]=studentDetail. But I don't understand why?

Thanking you in advance.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You don't have to guess where the error is occuring. The error message tells you exactly which line causes the problem:
Storage.storeStudent(StorageTest.java:127)

This means that the error occurs in the file StorageTest.java on line 127 in the storeStudent() method in the Storage class. So, I think you are right that the line causing the problem is:

Part of the issue is that you are using the variable hash directly. Typically, a hash function cannot guarantee that the hash code is a valid index into the container that uses the hash code. A typical solution is to use the modulus operator (%) to ensure it is the correct range:

However, this causes additional problems. Assuming your array size is 10, what happens if one Student object has a hash code of 22 and another has a hash code of 52? Notice that 52 % 10 and 22 % 10 are BOTH 2!

This is called a collission. There are various methods to deal with this, but I'll leave it to you to google for a solution. There is plenty of literature available on hash tables that will explain it more clearly than I can.

I hope this explains the cause of the problem and also helps you get along the way to find a solution. I'd like to note that Java already implements many containers that take care of these kinds of details for you. However, it's always a good educational experience to implement them for yourself. Good luck!

Layne


Java API Documentation
The Java Tutorial
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Layne, many thanks for your response.

The offending line of code: students [hash] = studentDetail;
is calculated on another method HashIndex in another class called Student. i.e.


I don't understand why I should have a collision though, as I have only entered one student when I get the error message?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Storage.storeStudent(StorageTest.java:127)
at StorageTest.main(StorageTest.java:182)




Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hi,

Worked this out for myself. Was calling the HashIndex twice on the same name, hence the collision.

A special huge thank you to Dun Dagna for staying with me on this!
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
OK Maureen, so I will repost my program with some comments to explain
what is going on in each part of the code. This still compiles
and runs OK for me; please try it yourself.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hello again Dun Dagna,

I agree, I managed to compile your code no problem however I had a run time error of the following;

C:\java>java StorageTest
Testing
Student name: Sally Collins Student course: Visual C#
Testing
Exception in thread "main" java.lang.NullPointerException
at StorageTest.main(StorageTest.java:40)

The offending line of code is:


I have tested the Student class and have no problems.
In my Storage class I have declared a variable:

In my Storage class I have a constructor, the same as you as follows:



I then have the method store:


AND the method retrieveStudent


In my main program I declare my Student objects and pass them to the storeStudent method. But unfortunately I am getting an array when I try to output three Students. My array size declared in the main program is not the problem and the Student class tested ok so I'm guessing (after trying many things over the last few days)that once again it is to do with my storeStudent method in Storage class but I can't see why? Are you able to enlighten me once more? Or alternatively pass me in the right direction?

Thanking you again!
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi Maureen,

This is due to the data collision problem I mentioned. The names "Joe Bloggs", "Fred Smyth" (with a y) and "Sally Collins" do not cause this problem, because they all evaluate to a different hashCode. However, pretty much any other set of names will give you trouble.

One of the main objectives of this assignment from Computeach is to get you to devise your own code for circumventing this problem, and therefore to make you appreciate how useful the java.util.Collections classes are, whenever you come to use those, since they take care of this problem for you (by and large).

To avoid data collision, you need to insert code in your storeStudent() method that will:

1) check if the array position specified by the hash code is available for use.
2) If it isn't available (e.g. already taken by a different Student object), then search for the next available position and store the object there.

Each time you call the storeStudent method with a Student object, it should carry out the above steps before attempting to store the object. That way you should avoid collisions.

See how you get on with that, post again if you are stuck.

DD
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi Maureen,

Looking at your own code again, I see that you are making a lot of your methods and variables static. There should be no need for you to do this, as you should be calling these methods on instances of the various Student objects and the Storage object created by your program, so Java should be able to find them and use them without needing to make them static.

In fact, making variables static in this way can interfere with correct operation of your code, as a static variable only has one instance per class, so all objects made from that class will all be looking at the same variable. This is probably not a good idea in your case, as you want your Student objects to be separate from each other, storing different name and course information for each Student.

DD
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Dun Dagna, again many thanks for staying with me on this.

With regard to the collisions (which I understand I need to tackle but for now I am leaving that until one of the many things I need to do later), I have ensured that no collisions are apparent. I have tested this and get the following hashindex's:

C:\java>java StudentTest

The student Joe Bloggs =hash index 7

The student Fred Smyth =hash index 3

The student Sally Collins =hash index 0

I have also ensured that static is not apparent (- can't believe I left these in, after editing the code over the weekend to attempt to find the problem, when I actually answered a thread about static methods this morning).

The error message I get now, when I print out is:
C:\java>java StorageTest
Testing
Student name: FRED SMYTH Student course: Visual Basic
Testing
Exception in thread "main" java.lang.NullPointerException
at StorageTest.main(StorageTest.java:39)

The offending line of code is:
System.out.print("Student name: " +output.getName ( ) );

So I looked at the getName method of the Student class which just returns name.

I also looked at the setName method of the Student class which just says name is = studentName.

However, I notice that the setName method allows the contents of name to be changed to store a different String value, should that be required and since I change the name variable to upper case in my Student hashCode method:


I thought I would have a look again at my understanding of get and set. Unfortunately I am finding very little. My course notes just say get and set are straightforward. "The get methods return the value of a member they are associated with and the set method sets the member to a new value. Are you able to elaborate here or alternatively suggest where I can find out more?
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi Maureen,

Just a couple of suggestions: why are you subtracting one from the hash index? In the case where a Student name's total ASCII codes add up to a multiple of the number of students to be stored, the expression
totalASCII % numStudents will give a value of zero. If you then subtract 1 from this, you will get -1, which is not a valid index to use in an array (you will get an ArrayIndexOutOfBoundsException and crash the program).
So that is one thing. The other is that you are converting the name to upper case before adding up its ASCII codes. This means that the string "Fred Smyth" will give a different hash code to "FRED SMYTH" as the ASCII codes for lower and upper case characters are different. This might cause your code to behave in unexpected ways.
See the ASCII table web page for a more complete explanation.

DD
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Dun Dagna - Anyone tell you you're great!

I have done it!
(Not the assignment - still got A few more methods and classes to write but I have achieved and understood this part. Thanks to you for helping. I would of given up on java altogether if it wasn't for you explaining things to me and giving me things to think about. I can't thank you enough.
I've even helped some other people on their threads; although I must admit I'm pleased when someone more senior has a read in case I haven't explained things very well but I do find I learn even more from helping others as it makes me think about it as well).

C:\java>java StorageTest
Student name: JOE DOHN Student course: Visual Basic
Student name: SALLY COLLINS Student course: History
Student name: JOE DOHN Student course: Visual C#
Student name: JOE BLOGGS Student course: Java
Student name: FRED SMYTH Student course: Visual Basic
Student name: JOHN DOE Student course: Java

What can a say......... Thank you doesn't express my gratitude enough to you for staying with me on this!!!

Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi Maureen,

That's fantastic news, well done! I see you have been helping others out as well, so good for you!

I found these initial OOP conceptual hurdles that you have run into to be the hardest part of learning Java that I have encountered so far. The rest seems to be just remembering what the various packages do and applying the appropriate class and/or method to the problem at hand.

Once you submit and pass your first assignment (concentrate on writing up a good Test Plan and documenting your code well, the tutors like that), you will be invited by Computeach to attend an in-centre 2-day Java workshop at their training centre in Dudley, near Wolverhampton. This is a good way of getting to know your tutors and of meeting a few other students (there are usually 3 or 4 other people there at the same time). I found the course great for getting answers to questions face-to-face from qualified Java professionals and for making me more confident in my coding.

So once again, congratulations and very well done!!!
Thanks also for your kind comments, I like to help people out if I can.
DD



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays: Calling input from other methods and classes