如何防止覆盖并添加到现有数组(java)

wqsoz72f  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(411)

我的项目主要按照预期工作,除了我的方法将覆盖初始条目而不是将新条目添加到数组中这一事实。
例如,如果我通过选项1输入2个条目,然后尝试通过选项2添加另一个(单个)条目,索引0将被新条目覆盖。
我已尝试将类设置为“final”以防止覆盖,但不确定在使数组具有附加功能方面会出现什么错误:

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();
    }
}
toiithl6

toiithl61#

所以问题是,每次插入数组时,都从索引0开始。解决方案是在数组中找到第一个空索引位置,并从该索引开始填充,以便每次添加到新索引时,而不是覆盖已添加的值。为了找到第一个空索引位置,我添加了方法findFirstEquinIndex(),并修改了两个方法multiemployee()和singleemployee(),以使用新添加的方法。

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();
    }
}

相关问题