Home > Software design >  Is it possible to keep objects inside an array after breaking a while loop in Java?
Is it possible to keep objects inside an array after breaking a while loop in Java?

Time:07-31

I am trying to store objects (employee objects) inside an array. I have a while loop running an interface asking what the user wants to do, if user wants to enter to the array database, then it will run another while loop, in this loop we ask the user if he/she wants to add a new employee or show the list of employees, after performing any of the two previous tasks, we get back to the first while loop, if we want to enter again to the loop to add/show the list of employees, the array deletes all the objects inside.

// Main class
import javax.swing.*;

public class Main {


    public static void main(String[] args) {
        
        while (true) {
            int opcion = Integer.parseInt(JOptionPane.showInputDialog(null, "(1) Enter to Database\n(2) Quit"));
            
            if (opcion == 1) {
                EmployeesFunction callingEmployeesConstructor = new EmployeesFunction();
            } else if (opcion == 2) {
                break;
            }
        }
    }
}
// Employees class
public class Employees {
    
    private String name;
    private boolean active;
    
    // Constructor
    public Employees() {
        this.name = "";
        this.active = false;
    }
    
    public Employees(String name) {
        this.name = name;
        this.active = true;
    }
    
    // Getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}
// Employee's Function class
import javax.swing.JOptionPane;

public class EmployeesFunction {
    Employees array[] = new Employees[5];
    
    // Constructor
    public EmployeesFunction() {
        array[0] = new Employees("Name"); // Initializing first object
        
        while (true) {
            int option = Integer.parseInt(JOptionPane.showInputDialog(null, "(1) Show list\n(2) Add new employee\n(3) Quit"));
            
            if (option == 1) {
                showEmployeesList();
            } else if(option == 2) {
                addNewEmployee();
            } else if (option == 3) {
                break;
            }    
        }
    }
    
    // Functions
    
    public void addNewEmployee() {
        int index = array.length - 1;
        int append_index = 0;
        for (int i=index; i>=0; i--) { // Append method such as Python
            if (array[i] == null || array[i].isActive() == false) {
                index--;
                array[i] = new Employees();
            } else {
                append_index = index 1;
                array[append_index].setName(JOptionPane.showInputDialog(null, "Write name"));
                array[append_index].setActive(true);
                break;
            }
        }
    }
    
    public void showEmployeesList() {
        String list = "";
        for (int i=0; i<array.length; i  ) {
            list  = array[i].getName()   "\n";
        }
        JOptionPane.showMessageDialog(null, list);
    }
}

So, after storing some objects inside the array, then returning to the first loop, and then entering again to see the list, the objects inside the array return to null.

CodePudding user response:

Your code has multiple issues, at first let me tell why you are getting that null object. As you said you're quitting to the first interface and then reentering, that means you are calling new EmployeesFunction() again. It is re-initialising the array. Hence the array will have only the entry with "Name" and the later indices will have null. Just after finishing adding the new employee, do not quit to the first interface, instead call the showEmployeeList, and you'll have them fine.

However,

You will face ArrayIndexOutOfBoundException if your declared 5 sized array is full and you try to add another entry in the employee array.

You're initialising the 0th index of the array with an entry "Name". Your code will never replace the first element as that is an active entry and will start appending from the second index (index 1). Hence your list will always have the first entry as Name.

Change your implementation and use a List. Add a new employee to the list if the list is empty or the last element of the list is not active. You'll overcome the aforementioned problems.

  • Related