Skip to content
Advertisement

How can I prevent an overwrite and add to exisiting array (Java)

My project mostly works as intended, save for the fact that my methods will overwrite the initial entry and not add the new entry to the array.

For instance if I input 2 entries via option 1, and then attempt to add another (single) entry via option 2, index 0 is overwritten by the new entry.

I’ve tried making the class “final” to prevent overwrite, but not sure where I am going wrong to make the array have additive functionality:

JavaScript

Advertisement

Answer

So the problem was that each time you were starting with index 0 while inserting in the arrays. The solution is to find the first null index position in the array and start filling from that index so that each time we add to new index instead of overriding the the already added values. To find the first null index position I added method findFirstEmptyIndex() and modified the 2 methods MultiEmployee() and SingleEmployee() to use the newly added method.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement