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 Adding object to array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding object to array" Watch "Adding object to array" New topic
Author

Adding object to array

Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi in the programe below when i run it prints to the screen this:
student.assignment.Student@108786b
student.assignment.Student@108786b
student.assignment.Student@108786b

i think it is some thing to do with the code students[index] = studentObj;
in the addStudent method in the storage class can any one inlighten me on to why this is happing?

here is the code:
package student.assignment;

public class Student
{
private String name;

public Student(String newName, String newCourse)
{
name = newName;
}
public String getName()
{
return name;
}
}

package student.assignment;

public class Storage
{
private Student[] students;

public Storage(int maxNum)
{
students = new Student[maxNum];
}
public void addStudent(Student studentObj)
{
int index=0;
for(index=0; index<students.length; index++)
{
students[index]= studentObj;
}

}
public Student showAll(int index)
{
return students[index];
}
}


package student.assignment;

import javax.swing.JOptionPane;

public class StudentTest
{

public static void main(String[] args)
{
int maxNum=3;
int count=0;
Storage store = new Storage(maxNum);
do
{
String name = JOptionPane.showInputDialog("please enter a name!");
Student stu = new Student(name, name);
store.addStudent(stu);
count++;
}
while(count!=maxNum);
{
for(int i = 0; i<maxNum; i++)
{
System.out.println(store.showAll(i));
}
}

}

}


Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
It looks like you need to override the toString() method in the Student class.


-Rich, SCJP 1.4
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi Richard
thank you for your reply can you explain a little? much appriciated.
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Well, if you want an object's properties to be printed out you have to override the toString() method found in the Object class.

Example:

You'd need to put this method in the Student class.

public String toString() {
System.out.println("Student name=" + name);
}

You have to override toString() if you want to provide some meaningful information, otherwise it will just print the object reference, which is usually totally useless. ;-) That looks like what you have printing out now, the object reference.

Have fun!
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Whoops....my post was wrong! The method is like the following:

public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Student name=" + name);
return sb.toString();
}

Sorry!
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Martyn,
There are a few changes you need to make to your code, inorder that it shows up meaningful results.

Firstly, I did not understand why you had you a for loop in the addStudent() method in the Storage class, I have removed the for loop, and instead used the counter from your StudentTest class. The changes are shown below, see the commented part,


Because of the above for loop which has been commented I think you were getting the same Student object printed out all the time. It was your last student object that was getting printed, because you set the index to zero and added the last Student object to all the indexes of the Student array. Just try to think over the logic you will get the answer.

Next, the problem with the student objects not showing up properly, you need to make the following change here in your StudentTest() class.


Just call the getName() method on your student object, that will display the names.

The entire code is given below, it works fine. I have removed some of the package statements and eased the access modifiers to make it easy on me.
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi everyone!
thanks for all your posts, this really helps and Abdulla thank you for explaing to me what was i wrong your code works great and i now understand where i was going wrong on this. My Problem solved....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding object to array