| | |
Beginner needs help with 2d array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 0
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. 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)
final int ROWS = 3; final int COLS =26; String[][] employees = new String[ROWS][COLS]; for(int row=0; row<ROWS;row++){ for(int col=0; col<COLS; col++){ System.out.println("Enter an employee"); String number = input.nextLine(); employees[row][col]=number; } } for(int row=0; row<ROWS;row++){ for(int col=0; col<COLS; col++){ system.out.println(employees[row][col]); } }
Last edited by carlcarman; Nov 23rd, 2008 at 4:43 pm.
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.
the way you describe it you want to enter the employee name in the outside loop and the payments in the inside loop.
•
•
•
•
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
Last edited by Paul.Esson; Nov 23rd, 2008 at 6:59 pm.
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 )
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
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.
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)
String[][] employees = new String[ROWS][COLS]; for(int row=0; row<ROWS;row++){ System.out.println("Please enter Employee Name :"); String name = input.nextLine(); employees[row][0]=name; for(int col=1; col<COLS; col++){ System.out.println("Please enter Employee: " + row + " Pay: " + col); String number = input.nextLine(); employees[row][col]=number; } }
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.
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.
![]() |
Similar Threads
- Beginner array help (C++)
- organizing array output (Java)
- Help please beginner in C (C)
- beginner"problem in arrays help me in codes" (C)
- beginner (C++)
- Array problem (C)
Other Threads in the Java Forum
- Previous Thread: Help with animation
- Next Thread: Please help
| Thread Tools | Search this Thread |
addball addressbook android api applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie output phone physics print problem program programming project radio recursion reporting scanner screen se server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech utility windows





