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:

import java.util.Scanner;

public final class ProjectTest {

    //Create Method Arrays
    final static int [] EmployeeID = new int[99];
    final static double [] EmployeeSalary = new double [99];
    final static String [] EmployeeFirst = new String [99];
    final static String [] EmployeeLast = new String [99];
    private static Scanner scan;

    //Method Add MultiEmployee
    private final static void MultiEmployee () {
        scan = new Scanner(System.in);
        System.out.println("nHow many employees would you like to enter?: ");  
        int EmployeeCount = scan.nextInt();
        for (int i = 0; i < EmployeeCount; i++) {
            System.out.println("nEmployee " + (i+1)+":");
            System.out.println("nEnter employee name (First Last):");
            EmployeeFirst[i] = scan.next();
            EmployeeLast[i] = scan.next();
            System.out.println("nEnter Employee ID#:");
            EmployeeID[i] = scan.nextInt();
            System.out.println("nEnter Employee Salary:");
            EmployeeSalary[i]=scan.nextDouble();
        }
    
    }

    //Method Add SingleEmployee
    private final static void SingleEmployee () {
        for (int i = 0; i < 1 ; i++) {
            scan = new Scanner(System.in);
            System.out.println("nEmployee " + (i+1)+":");
            System.out.println("nEnter employee name (First Last):");
            EmployeeFirst[i] = scan.next();
            EmployeeLast[i] = scan.next();
            System.out.println("nEnter Employee ID#:");
            EmployeeID[i] = scan.nextInt();
            System.out.println("nEnter Employee Salary:");
            EmployeeSalary[i]=scan.nextDouble();
        }
    }

    //Method ReturnAll
    private final static void ReturnAll () {
        for (int i = 0; i < 99; i++) {
            if (EmployeeFirst[i]==null) {
                break;
            }
            else if (EmployeeID[i] >= 0) {
                System.out.println("nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +"      " + "Employee ID: "+EmployeeID[i]+"      " +"Employee Salary: "+ EmployeeSalary[i]);
            }
        }
    }

    //Method ReturnByID
    private final static void ReturnByID () {
        scan = new Scanner(System.in);
        System.out.println("nEnter the employee ID# for data you wish to retrieve:");
        System.out.println("nEmployee ID#: ");
        int EmpSearch = scan.nextInt();
        for (int i = 0; i<99; i++) {
            if (EmpSearch == EmployeeID [i]) {
                System.out.println("nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +"      " + "Employee ID: "+EmployeeID[i]+"      " +"Employee Salary: "+ EmployeeSalary[i]);       
            }
        }
    }

    //Method ReturnBySalary
    private final static void ReturnBySalary () {
        scan = new Scanner(System.in);
        System.out.println("nEnter the lower salary boundary:");
        int LowSal = scan.nextInt();
        System.out.println("nEnter the upper salary boundary");
        int HighSal = scan.nextInt();
        for (int i = 0; i<99; i++) {
            if (EmployeeFirst[i]==null) {
                break;
            }
            else if (EmployeeSalary[i] >= LowSal && EmployeeSalary[i] <= HighSal) {
                System.out.println("nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +"      " + "Employee ID: "+EmployeeID[i]+"      " +"Employee Salary: "+ EmployeeSalary[i]);       
            }
        }
    }

    
    //Main Method
    public static void main(String[] args) {

        //Call Scanner
        Scanner scan = new Scanner(System.in);
        
        //User Welcome Prompt and Menu
        System.out.println("Welcome to the Employee Database.");
        System.out.println("nThis program will accept employee names, ID number, and salaries.");
        System.out.println("nThen will return employee information by using Employee ID or salaries by range.");
        int MenuSelect;
        do {
            System.out.println("nnMain Menu:");
            System.out.println("n1. Load multiple employees data.");
            System.out.println("n2. Load data for one employee.");
            System.out.println("n3. Return data for all employees.");
            System.out.println("n4. Search and return for employee by employee ID.");
            System.out.println("n5. Search for employees within salary range.");
            System.out.println("n6. Exit Program.");
            System.out.println("nYour selection:");

            MenuSelect = scan.nextInt();

            if (MenuSelect == 1) {
                MultiEmployee();
            }

            else if (MenuSelect == 2) {
                SingleEmployee ();
            }
            else if (MenuSelect == 3) {
                ReturnAll(); 
            }
            else if (MenuSelect==4) {
                ReturnByID();
            }
            else if (MenuSelect ==5) {
                ReturnBySalary ();
            }
            else if (MenuSelect == 6) {
                System.out.println("nExit Program...Good-bye.");
                break;
            }
        } while (MenuSelect < 6);

        //Scan Close
        scan.close();
    }
}

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.

import java.util.Scanner;

public final class ProjectTest {

    //Create Method Arrays
    final static int [] EmployeeID = new int[99];
    final static double [] EmployeeSalary = new double [99];
    final static String [] EmployeeFirst = new String [99];
    final static String [] EmployeeLast = new String [99];
    private static Scanner scan;
    
    public static int findFirstEmptyIndex() {
        for(int i = 0;i<EmployeeFirst.length;i++) {
            if(EmployeeFirst[i]==null)
                return i;
        }
        return -1;
    }

    //Method Add MultiEmployee
    private final static void MultiEmployee () {
        scan = new Scanner(System.in);
        System.out.println("nHow many employees would you like to enter?: ");  
        int EmployeeCount = scan.nextInt();
        int firstEmptyIndex = findFirstEmptyIndex();
        for (int i = firstEmptyIndex; i < firstEmptyIndex+ EmployeeCount; i++) {
            System.out.println("nEmployee " + (i+1)+":");
            System.out.println("nEnter employee name (First Last):");
            EmployeeFirst[i] = scan.next();
            EmployeeLast[i] = scan.next();
            System.out.println("nEnter Employee ID#:");
            EmployeeID[i] = scan.nextInt();
            System.out.println("nEnter Employee Salary:");
            EmployeeSalary[i]=scan.nextDouble();
        }
    
    }

    //Method Add SingleEmployee
    private final static void SingleEmployee () {
        int firstEmptyIndex = findFirstEmptyIndex();
        for (int i = firstEmptyIndex; i < firstEmptyIndex + 1 ; i++) {
            scan = new Scanner(System.in);
            System.out.println("nEmployee " + (i+1)+":");
            System.out.println("nEnter employee name (First Last):");
            EmployeeFirst[i] = scan.next();
            EmployeeLast[i] = scan.next();
            System.out.println("nEnter Employee ID#:");
            EmployeeID[i] = scan.nextInt();
            System.out.println("nEnter Employee Salary:");
            EmployeeSalary[i]=scan.nextDouble();
        }
    }

    //Method ReturnAll
    private final static void ReturnAll () {
        for (int i = 0; i < 99; i++) {
            if (EmployeeFirst[i]==null) {
                break;
            }
            else if (EmployeeID[i] >= 0) {
                System.out.println("nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +"      " + "Employee ID: "+EmployeeID[i]+"      " +"Employee Salary: "+ EmployeeSalary[i]);
            }
        }
    }

    //Method ReturnByID
    private final static void ReturnByID () {
        scan = new Scanner(System.in);
        System.out.println("nEnter the employee ID# for data you wish to retrieve:");
        System.out.println("nEmployee ID#: ");
        int EmpSearch = scan.nextInt();
        for (int i = 0; i<99; i++) {
            if (EmpSearch == EmployeeID [i]) {
                System.out.println("nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +"      " + "Employee ID: "+EmployeeID[i]+"      " +"Employee Salary: "+ EmployeeSalary[i]);       
            }
        }
    }

    //Method ReturnBySalary
    private final static void ReturnBySalary () {
        scan = new Scanner(System.in);
        System.out.println("nEnter the lower salary boundary:");
        int LowSal = scan.nextInt();
        System.out.println("nEnter the upper salary boundary");
        int HighSal = scan.nextInt();
        for (int i = 0; i<99; i++) {
            if (EmployeeFirst[i]==null) {
                break;
            }
            else if (EmployeeSalary[i] >= LowSal && EmployeeSalary[i] <= HighSal) {
                System.out.println("nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +"      " + "Employee ID: "+EmployeeID[i]+"      " +"Employee Salary: "+ EmployeeSalary[i]);       
            }
        }
    }

    
    //Main Method
    public static void main(String[] args) {

        //Call Scanner
        Scanner scan = new Scanner(System.in);
        
        //User Welcome Prompt and Menu
        System.out.println("Welcome to the Employee Database.");
        System.out.println("nThis program will accept employee names, ID number, and salaries.");
        System.out.println("nThen will return employee information by using Employee ID or salaries by range.");
        int MenuSelect;
        do {
            System.out.println("nnMain Menu:");
            System.out.println("n1. Load multiple employees data.");
            System.out.println("n2. Load data for one employee.");
            System.out.println("n3. Return data for all employees.");
            System.out.println("n4. Search and return for employee by employee ID.");
            System.out.println("n5. Search for employees within salary range.");
            System.out.println("n6. Exit Program.");
            System.out.println("nYour selection:");

            MenuSelect = scan.nextInt();

            if (MenuSelect == 1) {
                MultiEmployee();
            }

            else if (MenuSelect == 2) {
                SingleEmployee ();
            }
            else if (MenuSelect == 3) {
                ReturnAll(); 
            }
            else if (MenuSelect==4) {
                ReturnByID();
            }
            else if (MenuSelect ==5) {
                ReturnBySalary ();
            }
            else if (MenuSelect == 6) {
                System.out.println("nExit Program...Good-bye.");
                break;
            }
        } while (MenuSelect < 6);

        //Scan Close
        scan.close();
    }
}
Advertisement