提问者:小点点

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


我的项目基本上按预期工作,除了我的方法将覆盖初始条目而不是将新条目添加到数组中的事实。

例如,如果我通过选项1输入2个条目,然后尝试通过选项2添加另一个(单个)条目,则索引0将被新条目覆盖。

我尝试将类设为“最终”以防止覆盖,但不确定我在哪里出错以使数组具有附加功能:

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

共1个答案

匿名用户

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

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