0

O.K. I'm green at this Java. But first problem is how to make the sentinel stop while entering elements in a 2d array from a scanner? Next, How do you search this array for a value a user inputted through a scanner then display the elements, row and column?

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

    int totalEmp = 10;                                          //Variables
    int totalID = 2;                                            //Variables
    String empNames;
    String IDSearch;


    String[][] employees = new String[totalEmp][totalID];       //Create Array

    
    //Instructions to input employee name and ID
    System.out.println("Enter employee name and ID for five employees.");
    System.out.println("ID must begins with B followed by 4 numbers.");
    System.out.println("Example: B0001, B2354 \n");
    


    //For loop to add name and ID, with sentinel to stop loop           
    for (int row = 0; row < employees.length; row++)
    {
        Scanner names = new Scanner(System.in); //New Scanner Object of Keyboard    
            System.out.print("Enter " + (row + 1) + " Name: (ZZZ to stop)");                
            employees[row][1] = names.nextLine();

            if (!employees[row][1].equals("ZZZ"))
            {               
                System.out.print("Enter employee ID (ex.:'B0000') ");
                employees[row][0] = names.nextLine();
            }
            else 
                
            System.out.println("You have exited entering names and ID's");                          
                    continue;
        }
    Scanner ENTER = new Scanner(System.in);
    System.out.println("You have completed entering names, press ENTER to continue. ");
    ENTER.nextLine();       
    showNames(employees);
    
    Scanner Search = new Scanner(System.in);
    System.out.println("\nEnter an Employee ID number to search for name. ");
    IDSearch = Search.nextLine();       
}

    
private static void showNames(String[][] employees)
{
    System.out.println("\n\nEMPLOYEE NAMES AND ID'S ENTERED: ");
    System.out.println("______________________________________");
    
    for (int names = 0; names < employees.length; names++)
    {
        System.out.printf("Employee Name: " + employees[names][1] + "   -   ");     
        
        for (int id = 0; id < 1; id++)
        {
            System.out.print("Employee ID: " + employees[names][0]);                
            System.out.println();   
        }
    }
    System.out.println("________________________________________________________\n");
    System.out.print("\nYOU HAVE ENTERED ** " + employees.length + " ** EMPLOYEES.");
    int ArraySize = employees.length;
}

public static int[] EmployeeSearch(String[][] employees, String IDSearch, int ArraySize) {

} }

I've tried the do while, which it doesn't seem to recognize the string. I've tried the If within the loop and same thing. As far as the linear search, I'm lost! Everything shows 1D arrays of ints.

0 Answers0