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 and methods - not getting the results I expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays and methods - not getting the results I expected" Watch "Arrays and methods - not getting the results I expected" New topic
Author

Arrays and methods - not getting the results I expected

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have a class called main program. Another two called Student and Storage.

In my main program I am getting the name of the student the course they are enrolled on.

//This is in a for loop

//This is the end of the for loop.

I then want to do other things with the student details and then at the end, of the MainProgram I wish to print out the student details; so I call my method display in the Storage class to do this.



No problems with the above I feel....

However, what I am getting is the following:

Student Name: Student Course:
============= ==============+

sue sewing
sue sewing

It should of been:
fred maths
sue sewing

The method in my Storage class, called display is the following:


I don't understand why I am not getting the first studentDetails and then the second studentDetails? Why am I getting the second studentDetails twice?

Is anyone able to enlighten me? Am I not understanding something here?

Thanking you in advance.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Not enough code to tell at this point, but I can give you a few pointers.

It seems there's a bug either in how you're filling the array, storing a Student or retrieving a Student, but snice you test all three at once, you cannot tell. You need to locate the bug first.

To help you could modify your display method like this:Doing this should tell you if the bug is in the code that builds the array or in the Storage object. If the latter, you'll need to delve deeper to determine whether the problem is in storeStudent or retrieveStudent.

If you can't find the problem, post the Storage class as that's likely where the trouble is.

Oh, you also said you have "studentDetails [lp] =new Student (name, course);" in a for loop. Is the code that reads name and course also in the for loop? If not, you're creating multiple Students with the same information (likely the last read values). This would definitely give you the results you're seeing.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hi again,

The Student and Storage class have both been tested separately and displayed as expected. I'm pretty confident that these are ok.

The MainProgram differs from my testing in that I use the JOptionPane to obtain the student name and course. Previously I entered the details in.

In the tests for the Student and Storage class I made some Student objects for Storage
Student studentDetail1 = new Student ("Joe Bloggs", "Java");
Student studentDetail2 = new Student ("Fred Smyth", "Visual Basic");

I then made an instance of Storage class to store Student object references in.
Storage store = new Storage(number)
store.storeStudent (studentDetail1);
store.storeStudent (studentDetail2);

I now want to use the JOptionPane in my MainProgram to obtain the studentDetails.

So my MainProgram does the following after using the JOptionPane:


My display method in the Display class has the following code:


BUT the output I am getting is not exprected and I really can't understand why?

Stored : Joe Bloggs java
Retrieved: Joe Bloggs java
Stored : Fred Smyth Visual Basic
Retrieved: Fred Smyth Visual Basic

//The output from calling the display method:
Stored: Fred Smyth Visual Basic
Retrieved: Fred Smyth Visual Basic

Stored: Fred Smyth Visual Basic
Retrieved: Fred Smyth Visual Basic

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Maureen Charlton:
//The output from calling the display method:
Stored: Fred Smyth Visual Basic
Retrieved: Fred Smyth Visual Basic

Stored: Fred Smyth Visual Basic
Retrieved: Fred Smyth Visual Basic
Okay, so that tells me that the Storage class seems to be working as you say, and the trouble lies in building the studentDetails array passed to display().

Is the studentDetails array from the first block of code the same array that you pass to display()? And is lp the for loop counter? I can't tell because you cut off the top of the loop. I assume there's no chance that "store.storeStudent (student)" is modifying the student itself.

At this point I'd start putting System.out's throughout to verify your assumptions along the way, or you could use a debugger. If you're still stuck, try posting more code context.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays and methods - not getting the results I expected