aspose file tools*
The moose likes Beginning Java and the fly likes printing out all my instances...toString() method.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "printing out all my instances...toString() method.." Watch "printing out all my instances...toString() method.." New topic
Author

printing out all my instances...toString() method..

N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
Very green greehorn here...please forgive if I don't use the correct programming terms below...

I created a Student class file that that compiled fine and contains this toString() method:


I am currently working on a StudentTest class file and am trying print out all instances of my student object (is that the way to say it?) using the toString() method. The file compiles but the student data doesn't print out.

Here's the code so far:


Please help....so confused!
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Where are you trying to call the toString() method on the Student object ?
Is it here,


But the array args[] contains command line arguments as data.

To make use of the toString method in the Student object, you need something like,
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
I'm sorry, but I don't understand. I'm more confused than ever...

I'm calling the toString() method but want to print all the data for each student (is it right to say each instance of the Student object?)

I was able to print out the data for 'student1' with this:

but I thought I could use a for loop so I could print them all out.
Barend Garvelink
Ranch Hand

Joined: Dec 07, 2004
Posts: 64
<hr></blockquote>

The problem is that you created 6 student instances (that is the right word, btw) that you names student1 through student6. To print out the data for each student, you must refer to them by the names you gave them, e.g.:



of course, this gets impractical really quickly. What if you have to handle 32 students instead of 6? Wat if there are 128, or a thousand?


The for-construct that you wrote (the last bit that I left quoted) is very well-suited to printing out a list of students without knowing beforehand how long that list is. The problem is that you're using it to print the wrong list! .

So, without giving away exactly how you should do it, here's what you should do:
  • Create an array in which you can store Student objects, make sure it's the right size to store six of them.
  • Store your six students in that array, instead of naming them student1...student6 as you do now.
  • Change your for-construct so that it goes over the array of Students, rather than over the array named "args".


  • [ July 24, 2005: Message edited by: Barend Garvelink ]

    SCJP 1.4, SCJD 1.4, SCWCD 1.3, ICSD:Websphere 5.1
    N. Gonzalez
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 29
    Sigh...this dunce cap on my head is getting heavy...

    Thank you for the guidance regarding the array.

    This is what I tried and it's not working:

    Any more hints would be appreciated. Thanks again.
    N. Gonzalez
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 29
    Oh, and I'm supposed to instantiate at least the objects of the student class using my constructors, which changed once I put Student objects in the array.

    so confused....!
    Barend Garvelink
    Ranch Hand

    Joined: Dec 07, 2004
    Posts: 64
    Originally posted by N. Gonzalez:
    public class StudentTest {

    public static void main (String[] args) {

    String [] studentarray = new String [studentarray.length];
    Student studentarray [0] = new Student {"Mary","Smith","111 Smith Street","New York","NY",10001,"msmith@nyu.edu","212-555-1111",true};
    Student studentarray [1] = new Student {"Bill","Brown","222 Brown street","New York","NY",10002,"bbrown@nyu.edu","212-555-2222",true};
    Studentstudentarray [2] = new Student {"Jill","Jones","333 Jones Street","New York","NY",10003,"jjones@nyu.edu","212-555-3333",true};
    Student studentarray [3] = new Student {"John","Martin","444 Martin Street","New York","NY",10004,"jmartin@nyu.edu","212-555-4444",true};
    Student studentarray [4] = new Student {"Bob","Wilson","555 Wilson Street","New York","NY",10005,"bwilson@nyu.edu","212-555-5555",true};
    Student studentarray [5] = new Student {"Betty","Boop","666 Boop Street","New York","NY",10006,"bboop@nyu.edu","212-555-6666",false};

    for (int i=0; i<studentarray.length; i++){
    System.out.println(studentarray[i].toString() + "\n");}
    }
    }



    Okay, two more problems in your code. First, this bit:

    String [] studentarray = new String [...];


    What you have defined here is an array in which you can put String objects. However, in that array you then try to put Student objects. These don't fit, cause they're a different kind of object. You have to change the array from String to Student, so that you can put Student objects in it.


    The other error is in the same line of code:
    String [] studentarray = new String [studentarray.length];


    Here you're making a new object, and telling it to be as big as itself... but it doesn't exist yet. You have to get the size of the object (that is, the number of students you're going to put into it) from somewhere else. For now, just put a number between the square brackets.
    N. Gonzalez
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 29
    I had just tried that right before I received your helpful response, but still it won't compile. I keep getting the following error message for each one of my Student objects in the array like:

    Abdulla Mamuwala
    Ranch Hand

    Joined: Jan 09, 2004
    Posts: 225
    Gonzalez, you have declared and constucted studentarray of type Student containg 6 Student objects, thats fine. Your code for that,


    Why are you declaring studentarray[0] to be of type Student again over here,


    Instead you should use the following code,


    Do that for all the Student objects, this is when you will be calling your Student constructor.
    N. Gonzalez
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 29
    still getting similar error message as before:

    StudentTest.java:37: '{' ir '[' expected


    what in the world is this referring to?

    arrrrgh!
    N. Gonzalez
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 29
    Same error message for every Student object in the array --6 errors total.
    N. Gonzalez
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 29
    Never mind....I fixed it.

    Thanks for all your help!

    Will post a new topic if I have further difficulty.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: printing out all my instances...toString() method..