permaculture playing cards*
The moose likes Beginning Java and the fly likes Array returning last value entered Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array returning last value entered" Watch "Array returning last value entered" New topic
Author

Array returning last value entered

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey yall, Im having a bit of trouble with this code. If I get past this then I think I can do the rest. My main method states that I want to retreive the 2nd students name in my array. When I run it, I only get the last person that was entered into the class, and not the 1st index. As you can see I have created 3 new students and the only one it will display is the last one I created. Im pretty sure it is my add method in the AClass class that is the problem. Any help would be appreciated.
Thanks in advance for the assistance.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steve,
I haven't completely analyzed your code, but your add method does appear to have a problem:

That for loop is setting every element in the studenet array to the Student object passed into the add() method. Somehow, I doubt that that is what you actually want.
Michael Morris
Does UBB stand for U been bit
[ March 22, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Mike, thanks for the reply, but you cant do what you suggested because it is a call to an array student.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
nm, I see that you fixed it. Still wondering how I need to fix mine though.
Thanks bro
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steve,
Yeah the damn UBB demon was making random changes to my post . Let me take a closer look at your code and see what I can come up with. Maybe you could explain exactly what you are trying to do with the AClass class.
Michael Morris
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Thanks Mike:
purpose of AClass class:
Create class named AClass that has:
o Data members: className (String), students(Student[25])
o Methods:
� Constructor(initialize className)
� Methods:
� String getName( ) //return className
� int find(Student s)//if s is in the array students, return the index of s (enrolled or not); otherwise return �1 (not in the array)
� Student retrieve(int n)//return the nth Student in the array
� int remove(Student s) //logically remove the student s from the class by setting his/her enrollment status to false (the student still physically being in the array until overwritten by method add. Return 1 if the operation succeeds; return �1 if the student was not enrolled in the class (in the array students)
� int add(Student s)// add a new student s into the class. If the student was removed from the class before, reset his/her enrollment status to true; otherwise, add this student into the class (overwrite a dropped student or add at the end of the array). Return 1 if the operation succeeds; return �1 class is full
� String toString()//return a String that has all info about all enrolled students in the class (call toString method of class Student)
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steve,
Since you have a well written and functional find method in the AClass, I suggest that you take advantange of that and use it in your add and remove methods. If I interpretted the requriements for the add and remove methods, then you probably want to do something like this:

Take a look at that and see if it makes sense to you.
Michael Morris
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
That last else block in the add method should set the student's status to true:

Michael Morris
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Thank you very much for the code Mike. I really appreciate you looking at this for me. Unfortunately, I got an error.
its a null pointer exception at a couple of the AClass find method lines
java.lang.NullPointerException
at exercise7.AClass.find(AClass.java:18)
at exercise7.AClass.add(AClass.java:78)
at exercise7.Test.main(Test.java:12)

Im going to keep working on this but Im really not getting anywhere. Apparently my find method isn't as good as its cracked up to be
Thanks for your time Mike. I sure would appreciate some more of it.
Thanks bro
Steve
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steve,
Yeah you need to use the numStudents in the for test instead of students.length:

Michael Morris
[ March 22, 2003: Message edited by: Michael Morris ]
[ March 22, 2003: Message edited by: Michael Morris ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
You da man Mike!!! I was looking at numStudents too and wondering how it was going to get up to the lenght of the array and as soon as I was going to ask it, you had the answer for me. But I have another quick question for you bro.
I have no idea how to do this.
Write statement(s) (call appropriate methods) to set the test scores of the 2rd student to the following array: 80.0 78.5 81.0 90.0 67.0
I know the method is already in place but I have no idea how to code the double values into the main method. So far I have dabbled and came up with:

plus I dont think my testscore methods are operational anyway. Me thinks I boloxed them up as well
Thanks again Mike. Now I can quit banging my head.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steve,
Let me guess, this is a college assignment? You do have some problems with the Student class but at first glance the setScore(double[]) method looks OK. You should be able to call it like this:

I'm assuming that you're a young college student full of nodoze and espresso. Unfortunately I'm an old fart and need my rest. Gotta go to bed, but I'll check this thread in the morning. As I said earlier you need to take a close look at your Student class as there are some obvious logic errors there.
Michael Morris
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Young would be kind. Thanks for helping me on this one Mike.
Steve
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
still nothing
this cant be all there is to the testscore array

going insane again. Why cant I just assign that array of doubles and apply it to the top method, and have the toString method in the Student class print it out?

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Still need some help, any would be appreciated.
Thanks guys and girls.
Steve
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steve,
Sorry for the lack of attention, but I have been taking advantage of the beuatiful spring weather here in East Texas.

Why cant I just assign that array of doubles and apply it to the top method, and have the toString method in the Student class print it out?

The getScore() method returns an array which is an object, so the toString() method returns something not very meaningful like [D@13f5d07. So you have to walk the array:

That will just put a space between each grade in the array. You may want to do some other formatting. If you are not famaliar with the StringBuffer class, it is very helpful in gathering several strings together into one big string.
Michael Morris
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Mike, now I just get all 0.0's, but i'll figure this one out. Unless you know something right off of the top of your head.
Thanks bro for all of your help...btw Im in San Antonio and yes the weather was awesome on Sunday.
Peace out
Steve
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I have modified my Student class as follows and I still return all 0.0's for the testscores. I am also including my main method.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I hope you guys don't mind if I chip in here a bit. The Student class looks okay, but I have a few comments about the following methods:

I see that in previous posts that you tried to use testscores=scores in the setScores() method, so I guess the above is probably the best way to go for now. However, in the getScore() method (should it be getScores() instead?) you don't need the for loop. It doesn't even do anything! If you had it for a reason at one point and then decided it wasn't necessary, perhaps commenting it out would be a good idea.
Okay, with that said, I would like to see how you call the setScores() method. Perhaps the array that you pass in doesn't have the values you think it does. It seems to me that the problem may be with whatever is using this Student class rather than with this class itself.


Java API Documentation
The Java Tutorial
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I figured it out. Dummy me forgot to declare the testscores array values before I tried to access them. I fixed them in the main method.
I have the original setScore method the way it was in the beginning (both of them.) And it works fine. There was no reason for me to walk the array in my setScore method, and I dont know what I was thinking in my getScore method. That again is just return testscores.
Thanks for your help on this one Mike and Layne. I owe you a case of Lone Star Mike
Anyway, problem resolved, and fart removed from brain.
Peace out bro's
Steve
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

I owe you a case of Lone Star Mike

Man that stuff ain't nothin' but bottled armydiller piss I drank one LS longneck when I was about 20 and vowed by the Yellow Rose of Texas I would never touch that stuff again. The two native Texas brews that I can handle is LBJ's ol' fave Pearl and of course Shiner Boch.
Michael Morris
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
aight, I owe you a case of Shiner. You know I was kidding about the lone star.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array returning last value entered