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();
}
}
Listinstead of anarray? The reason it happens is because when you callSingleEmployee()afterMultiEmployee()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.Listprevents you from doing that ? You can do the same things that you can do with anarraythe difference is that you don't have a fixed size with aListso you can add as many employees as you want and still acces it through their index.