Beginner needs help with 2d array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 22
Reputation: carlcarman is an unknown quantity at this point 
Solved Threads: 0
carlcarman carlcarman is offline Offline
Newbie Poster

Beginner needs help with 2d array

 
0
  #1
Nov 23rd, 2008
This is part of an ongoing project and up to now i've been able to figure things out. This insallment is:
1. Create an instance array called employees that will hold Employee elements.
2. Modify your loop to create a new element in the employees array for each employee entered by the user.
3. Create an input loop that allows the user to enter up to 26 payments per employee.
4. Allow the user to request a list of payments for a requested employee.
My code just keeps asking for employee name and I am trying to populate the array. I know I am missing it somewhere, can someone help me out? Here's my code
  1.  
  2. final int ROWS = 3;
  3. final int COLS =26;
  4.  
  5. String[][] employees = new String[ROWS][COLS];
  6. for(int row=0; row<ROWS;row++){
  7. for(int col=0; col<COLS; col++){
  8. System.out.println("Enter an employee");
  9.  
  10. String number = input.nextLine();
  11. employees[row][col]=number;
  12. }
  13. }
  14. for(int row=0; row<ROWS;row++){
  15. for(int col=0; col<COLS; col++){
  16.  
  17. system.out.println(employees[row][col]);
  18. }
  19. }
Last edited by carlcarman; Nov 23rd, 2008 at 4:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: carlcarman is an unknown quantity at this point 
Solved Threads: 0
carlcarman carlcarman is offline Offline
Newbie Poster

Re: Beginner needs help with 2d array

 
0
  #2
Nov 23rd, 2008
1
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Beginner needs help with 2d array

 
0
  #3
Nov 23rd, 2008
Its going to ask for the employee name 78 times. so only after that will it print out your array. I would try changing COL's to 3 or something so you don't have to enter in so many numbers before getting them printed out.

1. Create an instance array called employees that will hold Employee elements.
2. Modify your loop to create a new element in the employees array for each employee entered by the user.
3. Create an input loop that allows the user to enter up to 26 payments per employee.
4. Allow the user to request a list of payments for a requested employee.
My code just keeps asking for employee name and I am trying to populate the array. I know I am missing it somewhere, can someone help me out? Here's my code
the way you describe it you want to enter the employee name in the outside loop and the payments in the inside loop.
Last edited by Paul.Esson; Nov 23rd, 2008 at 6:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: carlcarman is an unknown quantity at this point 
Solved Threads: 0
carlcarman carlcarman is offline Offline
Newbie Poster

Re: Beginner needs help with 2d array

 
0
  #4
Nov 23rd, 2008
It is only supposed to have 3 entries for name(row) and 26 elements for data(column). I would gladly change the amount but my instructor wouldnt like it. Thanks for your reply.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Beginner needs help with 2d array

 
0
  #5
Nov 23rd, 2008
For the sake of debugging you can change it, Just have to change it back afterwalds.

Anyhow, It will take you 78 iterations and each time it will ask for an employee name, You don't want that I think. I think you are trying to get the pay for 26 of those iterations, now your not going to be able to store the name and the pay amounts in your array, to do that you will need COL+1 elements not COL ( Unless you want to store 25 pays )

  1. String[][] employees = new String[ROWS][COLS];
  2. for(int row=0; row<ROWS;row++){
  3. System.out.println("Please enter Employee Name :");
  4. String name = input.nextLine();
  5. employees[row][0]=name;
  6.  
  7. for(int col=1; col<COLS; col++){
  8. System.out.println("Please enter Employee: " + row + " Pay: " + col);
  9. String number = input.nextLine();
  10. employees[row][col]=number;
  11. }
  12. }

if you do somthing like that you will use the first col of each row for the name, The next COL-1 elements will then become the 'number' or pay.

Anyhow this is prolly not the best way of doing it, You really should have the pay as doubles and the names as Strings.

I think what your teacher is asking you to do when he says
1. Create an instance array called employees that will hold Employee elements.
is make a class called Employee ( that would have a name and an array of pays ) and create an array of type Employee that holds the employees.

I know if thats what he means he should not say Employee elements but say Employee Objects, but hey... I am pritty certain thats he/she is getting at.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: carlcarman is an unknown quantity at this point 
Solved Threads: 0
carlcarman carlcarman is offline Offline
Newbie Poster

Re: Beginner needs help with 2d array

 
0
  #6
Nov 23rd, 2008
Thanks for your help, The way you laid it out makes more sense to me than what I was doing. I appreciate it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC