Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need help with arrays in Java!

Options
  • 15-12-2013 4:58pm
    #1
    Closed Accounts Posts: 1


    So basically I have a question where I have to make a program that will permanently store 10 employees in an array and allow the user to input an new set of hours each week, which should be also stored in an array.

    the output screen should list the employees, their hours worked and their individual pay, total part time hours worked that week and sum of the payroll that week.

    I'm so **** with arrays, I don't even know where to start. Any help would be greatly appreciated!


Comments

  • Registered Users Posts: 8,671 ✭✭✭GarIT


    You could make an array of employee objects with all the info stored. Or if you haven't done anything with objects you could just use several arrays and just have position x in each array be for the same person.


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    For example, you could have the following (ignoring classes)

    int[] employeeID = new int[10];
    String[] employeeName = new String[10];
    int[] employeeHoursWorks = new int[10];
    etc for all attributes.

    So you would have 10 positions in each array, from 0 to 9. 0 would be employee one, one would be employee two etc. You just create a loop for (int i = 0; i < 10; i++) and enter in all the information for each employee. All attributes at position 0 would relate to the first employee, 1 for the second and so on.

    A much better way to do this is with classes and something like an ArrayList, for reasons of maintainability, extensibility and simple clean code, but don't worry about that for now. Just focus on getting it working with arrays first.


  • Registered Users Posts: 72 ✭✭shanard


    I would also suggest you work with an ArrayList - if you google it as well as refer to the API on oracle.docs....if you try the code and post your code here from scratch so we can see what you are doing and we will point you in the right direction.... :-)


  • Registered Users Posts: 201 ✭✭Plopli


    You're not alone ...


  • Hosted Moderators Posts: 7,485 ✭✭✭Red Alert


    I'd also recommend ArrayList with an Employee class as well. Or even just multiple ArrayList objects filled with String Worth getting to know it now:
    ArrayList<String> employeeNames = newArrayList<String>();
    ArrayList<Employee> employeeList = new ArrayList<Employee>(); 
    
    Employee employee = employeeList.get(0); // 1, 2, etc.
    

    Then use the append and length finding methods as detailed in the online docs.

    Your IDE should be providing completion help on such objects, if not, it's worth switching to Netbeans/Eclipse as they're designed to help you write Java.


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    If this were work then you'd have an Employee class and some sort of DOA class that will manage persistence. It would have utlity methods to provide me a collection of Employees. That collection is likey to be a List before it's an ArrayList!

    But if this were homework [and given that someone else just asked a very similiar question this is my bet] and I was asked to use an array to solve a problem and I used an ArrayList well I'd probably not score well since I,in essence, used a List rather than Array! If it were homework, I would think that the goal is to write your own implementation of ArrayList.

    If it's not homework, I would ask why must the employees be in an Array? Why not a List or some other collection? What is it that you believe an Array will offer that some other Collection will not?


Advertisement