943,540 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2470
  • Java RSS
Nov 23rd, 2008
0

Beginner needs help with 2d array

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 23rd, 2008
0

Re: Beginner needs help with 2d array

1
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 23rd, 2008
0

Re: Beginner needs help with 2d array

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.

Quote ...
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.
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Nov 23rd, 2008
0

Re: Beginner needs help with 2d array

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 23rd, 2008
0

Re: Beginner needs help with 2d array

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 )

Java Syntax (Toggle Plain Text)
  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
Quote ...
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.
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Nov 23rd, 2008
0

Re: Beginner needs help with 2d array

Thanks for your help, The way you laid it out makes more sense to me than what I was doing. I appreciate it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Using Scanner - I real need help!
Next Thread in Java Forum Timeline: Please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC