473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!


Expand|Select|Wrap|Line Numbers
  1.   private void mouseclickedbtnStoreInfo(java.awt.event.MouseEvent evt) 
  2. {                                          
  3. // Code to store info from the textbox entered by user:
  4.  
  5.  
  6.         s.setFirstName(txtFirstName.getText()); 
  7.  
  8.         s.setLastName(txtLastName.getText());
  9.  
  10.         s.setStudentID(txtStudentID.getText());
  11.  
  12.         arrayAllStudents[NumStu] = s;
  13.         NumStu++;
  14.  
  15.         txtFirstName.setText("");
  16.         txtLastName.setText("");
  17.         txtStudentID.setText("");
  18.  
  19.         //Testing array storage here also 
  20.  
  21. /*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.*/
  22.  
  23.     for(int i=0;i<NumStu;i++){
  24.             System.out.println(arrayAllStudents[i].getStudentID());
  25.             System.out.println(arrayAllStudents[i].getFirstName());
  26.             System.out.println(arrayAllStudents[i].getLastName());    
  27.       }
  28.  
  29.     }   
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.
Oct 19 '07 #1
3 1369
JosAH
11,448 Expert 8TB
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
Oct 20 '07 #2
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!
Oct 21 '07 #3
JosAH
11,448 Expert 8TB
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
Oct 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nayan Mansinha | last post by:
Hi All How can I store an array of objects in my C# Application.Settings? I have CMyObject class for which an array is created: CMyObject arr = new CMyObject; arr = new CMyObject();...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
13
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.