Storing in an array from my own created class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kjkrudh
    New Member
    • Sep 2007
    • 9

    Storing in an array from my own created class

    Hi,
    Here I am again with more questions. I am groveling for help.
    I am working on a program in Netbeans 5.5.1 where I created a class called Student that gets and sets Last Name, First Name and Student ID. Now the program to run as a samle of this class will store the first Student I get and put it into slot [0] of my array and then I increment my NumStudents, as a count to moveon to the next array slot. The problem is that it only puts the Student info into slot[0], even on the 2nd or 3rd entry to store information. I'm not sure if the problem is in the gets and sets in my class or in my Store Info button listener.
    Here is a bit of the code. I have comments to help explain my problem.

    Thank you oh virtuosos of Java! This stuff brings me to tears!


    [CODE=java] private void mouseclickedbtn StoreInfo(java. awt.event.Mouse Event evt)
    {
    // Code to store info from the textbox entered by user:


    s.setFirstName( txtFirstName.ge tText());

    s.setLastName(t xtLastName.getT ext());

    s.setStudentID( txtStudentID.ge tText());

    arrayAllStudent s[NumStu] = s;
    NumStu++;

    txtFirstName.se tText("");
    txtLastName.set Text("");
    txtStudentID.se tText("");

    //Testing array storage here also

    /*This only prints out only for the second student entered, loses the first, NumStu times, apparently only storing in slot 0. array is of Student class as is the variable s.*/

    for(int i=0;i<NumStu;i+ +){
    System.out.prin tln(arrayAllStu dents[i].getStudentID() );
    System.out.prin tln(arrayAllStu dents[i].getFirstName() );
    System.out.prin tln(arrayAllStu dents[i].getLastName()) ;
    }

    } [/CODE]

    Hopefully this is not a challenging puzzle for all you experts out there. Pop me a line soon. I'll take my turn helping when I get this #*@& figured out.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Create a new student every time you want to store a student in that array. As I
    can see from your code you're storing the same student 's' in every slot of your
    array; you're just changing it's name etc. over and over again so all slots end
    up pointing to the same student 's' which has the last changed name etc. in it.

    kind regards,

    Jos

    Comment

    • kjkrudh
      New Member
      • Sep 2007
      • 9

      #3
      Thank you so much. I don't quite understand that if the first Student is in my array already why it doesn't stay as the same information when I call for the next Student. Although, I do sort of get that the Student class is only holding the one Student object that I set. Anyway, I did set a new student at every action of my Store Info button and now it works great, just like you said.

      I appreciate the help. I know these basics must be a bore for you to see as questions and confusions with us beginners.
      Again, Thank you!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by kjkrudh
        Thank you so much. I don't quite understand that if the first Student is in my array already why it doesn't stay as the same information when I call for the next Student. Although, I do sort of get that the Student class is only holding the one Student object that I set. Anyway, I did set a new student at every action of my Store Info button and now it works great, just like you said.

        I appreciate the help. I know these basics must be a bore for you to see as questions and confusions with us beginners.
        Again, Thank you!
        You're welcome of course; let's play a strange little game: suppose you're an
        array with two elements; represented by your left and right hand. Also suppose
        I'm a student 's'.

        First your code gives me the name "Fred" and makes your first element slot
        point to me; so now your left hand points to me and my name is Fred. Next,
        your code changes my name to "Barney".

        Your left hand points to me, but my name is "Barney" now. Your code makes
        your second slot point to me as well. Now both your hands point to one and
        the same student: me. But my name is Barney now. So both your hands point
        to the same student 's;, named Barney.

        If you instead had created a new student object when you wanted to fill a next
        slot of the array and named *that* one Barney or whatever, your left and right
        hand would've pointed to two different students, both with their own name.

        kind regards,

        Jos

        Comment

        Working...