0

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("\n\nMain 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();
    }
}
5
  • Why don't you use a List instead of an array ? The reason it happens is because when you call SingleEmployee() after MultiEmployee() you don't check what is in your array, you just iterate through it starting at 0. So if there is already something at 0 you overwrite it. Commented Jul 8, 2021 at 9:44
  • Hello, I need to use an array here to be able to allow user input values, of whatever the user would like to input, rather than a fixed list. Commented Jul 8, 2021 at 9:52
  • How does a List prevents you from doing that ? You can do the same things that you can do with an array the difference is that you don't have a fixed size with a List so you can add as many employees as you want and still acces it through their index. Commented Jul 8, 2021 at 9:54
  • That's new knowledge to me (thank you for sharing), but in this case the project directions are that I have to use arrays still. Commented Jul 8, 2021 at 9:59
  • Then update your question so that we know that you have to use arrays ! I will answer your question in the next hours if someone hasn't done it by then. Commented Jul 8, 2021 at 10:23

1 Answer 1

1

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("\n\nMain 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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.