• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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 ]
     
    N. Gonzalez
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 64
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 225
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    still getting similar error message as before:

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



    what in the world is this referring to?

    arrrrgh!
     
    N. Gonzalez
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Same error message for every Student object in the array --6 errors total.
     
    N. Gonzalez
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Never mind....I fixed it.

    Thanks for all your help!

    Will post a new topic if I have further difficulty.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic