在数组(或ArrayList)中搜索由扫描仪用户指定的特定人(值),如果找到,则将该数组中的所有值返回/打印到扫描仪上

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在数组(或ArrayList)中搜索由扫描仪用户指定的特定人(值),如果找到,则将该数组中的所有值返回/打印到扫描仪上相关的知识,希望对你有一定的参考价值。

我制作了带菜单的交互式扫描仪。在主菜单选项1中,我希望用户输入名称或数字,然后在我的数组或ArrayList中搜索该特定值。如果找到,我想返回同一数组中的所有值,并打印到扫描仪。

我一直在一天的大部分时间里努力完成这项工作。 (请回答,就像您会成为新手一样)是我;)

我的主要:

        String[] employee1Array = {fitnessEmployee1.getName() + "       ", fitnessEmployee1.getCPR() + "   ", String.valueOf(fitnessEmployee1.getHours()) + "          ", String.valueOf(fitnessEmployee1.getSalary()) + "       ", fitnessEmployee1.getVacation()};
        String[] employee2Array = {fitnessEmployee2.getName() + "        ", fitnessEmployee2.getCPR() + "   ", String.valueOf(fitnessEmployee2.getHours()) + "          ", String.valueOf(fitnessEmployee2.getSalary()) + "      ", fitnessEmployee2.getVacation()};
        String[] employee3Array = {fitnessEmployee3.getName() + "        ", fitnessEmployee3.getCPR() + "   ", String.valueOf(fitnessEmployee3.getHours()) + "          ", String.valueOf(fitnessEmployee3.getSalary()) + "       ", fitnessEmployee3.getVacation()};
        String[] employee4Array = {fitnessEmployee4.getName() + "     ", fitnessEmployee4.getCPR() + "   ", String.valueOf(fitnessEmployee4.getHours()) + "          ", String.valueOf(fitnessEmployee4.getSalary()) + "      ", fitnessEmployee4.getVacation()};

        String[][] allEmployeesArray = {employee1Array,employee2Array,employee3Array,employee4Array};





        Scanner login = new Scanner(System.in);
        System.out.println("Please type in your username:");

        String userName = login.nextLine();
        if (userName.equals(userName1)) {
            System.out.println("Welcome " + userName);
        } else {

            System.out.println("Incorrect User - limited choices available");

        }


        String userChoice; 

        Scanner input = new Scanner(System.in);


        while(true){

            System.out.println("----Available Choices----");
            System.out.println(" Press 1 for search for a specific employee or member");
            System.out.println(" Press 2 to exit");

            System.out.println("
Please choose from the list:
");


            userChoice = input.next();

            String answer = "null";

            switch(userChoice){
                case "1":

                    System.out.println("Please write the name or CPR of the person you are searching for." );
                     answer = input.next();


                    System.out.println("Searching for " + answer);
                    for (int i = 0; i < allEmployeesArray.length; i++) {
                        System.out.println();

                            if (allEmployeesArray[i].equals(answer)) {
                                System.out.println(Arrays.toString(allEmployeesArray[i]));
                            }else{
                                System.out.println("Person not found");
                            }


                    }

                    System.out.println();
                    System.out.println("done with job number 1");
                    break;


                case "2":
                    //Afslut scanneren
                    System.out.println("Exiting...");
                    System.exit(0);
                default:

                    System.out.println("That is not a valid choice. Please try again...");
            }
        }



    }

}
答案

我通过创建重复事物的函数/方法在某种程度上简化了您的FitnessMain。请注意,由于引起混乱,我将FitnessEmployees类重命名为FitnessEmployee。以下是工作程序:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class FitnessMain {

    public static void udskrivArray(String[][] a) {
        for (int row = 0; row < a.length; row++) {
            System.out.println();
            for (int column = 0; column < a[row].length; column++) {
                System.out.print(a[row][column] + " ");
            }
        }

    }

    public static void udskrivArray2(String[][] a) {
        for (int row = 0; row < a.length; row++) {
            System.out.println();
            for (int column = 0; column < 2; column++) {
                System.out.print(a[row][column] + " ");
            }
        }
    }

    static void printEmployee(FitnessEmployee employee) {
        System.out.printf("%-12s", employee.getName());
        System.out.printf("%-14s", employee.getCPR());
        System.out.printf("%-12d", employee.getHours());
        System.out.printf("%-12s", employee.getSalary());
        System.out.printf("%-3s", employee.getVacation());
    }

    static String[] getMemberArray(FitnessMember fitnessMember) {
        String[] memberFieldsArr = { fitnessMember.getName(), fitnessMember.getCPR(), fitnessMember.getMemberType(),
                String.valueOf(fitnessMember.getFee()) };
        return memberFieldsArr;
    }

    static String[] getEmployeeArray(FitnessEmployee employee) {
        String[] employeeFieldsArray = { employee.getName(), employee.getCPR(), String.valueOf(employee.getHours()),
                String.valueOf(employee.getSalary()), employee.getVacation() };
        return employeeFieldsArray;
    }

    public static void main(String[] args) {

        int salaryAdmin = 23000;
        int salaryInstructor = 456;
        int hoursTove = 20;
        int hoursHenning = 15;
        // Employees data
        FitnessEmployee fitnessEmployee1 = new FitnessEmployee("Claus", "221175-1011", 37, salaryAdmin, "5",
                "Administrative");
        FitnessEmployee fitnessEmployee2 = new FitnessEmployee("Tove", "011080-1014", 20,
                salaryInstructor * hoursTove, "", "Instructor");
        FitnessEmployee fitnessEmployee3 = new FitnessEmployee("Anna", "011080-1012", 37, (salaryAdmin), "5",
                "Administrative");
        FitnessEmployee fitnessEmployee4 = new FitnessEmployee("Henning", "011080-1015", 20,
                (salaryInstructor * hoursHenning), " ", "Instructor");

        // Members data
        FitnessMember fitnessMembers1 = new FitnessMember("Morten", "130195-1303", "Full", 299, 1);
        FitnessMember fitnessMembers2 = new FitnessMember("Martin", "221175-1011", "Basic", 199, 2);
        FitnessMember fitnessMembers3 = new FitnessMember("Martina", "050970-1409", "Full", 299, 3);
        FitnessMember fitnessMembers4 = new FitnessMember("Marcel", "221175-1012", "Basic", 199, 4);

        System.out.println("FITNESS EMPLOYEES 
");

        System.out.printf("%-12s", "Name");
        System.out.printf("%-14s", "CPR");
        System.out.printf("%-12s", "Hours");
        System.out.printf("%-12s", "Salary");
        System.out.printf("%-3s", "Vacation
");

        System.out.println();
        for (int i = 0; i < 62; i++) {
            System.out.print("*");
        }
        System.out.println();

        printEmployee(fitnessEmployee1);
        printEmployee(fitnessEmployee2);
        printEmployee(fitnessEmployee3);
        printEmployee(fitnessEmployee4);

        // Array of some employees
        String[] employee1Array = getEmployeeArray(fitnessEmployee1);
        String[] employee2Array = getEmployeeArray(fitnessEmployee2);
        String[] employee3Array = getEmployeeArray(fitnessEmployee3);
        String[] employee4Array = getEmployeeArray(fitnessEmployee4);

        String[][] allEmployeesArray = { employee1Array, employee2Array, employee3Array, employee4Array };

        System.out.println();
        for (int i = 0; i < 45; i++) {
            System.out.print("=");
        }
        System.out.println();

        System.out.println("


FITNESS MEMBERS");
        System.out.printf("%-12s", "Name");
        System.out.printf("%-14s", "CPR");
        System.out.printf("%-12s", "Member type");
        System.out.printf("%-12s", "Fee");

        System.out.println();
        for (int i = 0; i < 62; i++) {
            System.out.print("*");
        }
        System.out.println();

        String[] member1Array = getMemberArray(fitnessMembers1);
        String[] member2Array = getMemberArray(fitnessMembers2);
        String[] member3Array = getMemberArray(fitnessMembers3);
        String[] member4Array = getMemberArray(fitnessMembers4);;

        ArrayList<String[]> fitnessMembersArrayList = new ArrayList<>();
        fitnessMembersArrayList.add(member1Array);
        fitnessMembersArrayList.add(member2Array);
        fitnessMembersArrayList.add(member3Array);
        fitnessMembersArrayList.add(member4Array);

        String[][] allMembersArray = { member1Array, member2Array, member3Array, member4Array };

        udskrivArray(allMembersArray);

        System.out.println("
");

        for (int i = 0; i < 45; i++) {
            System.out.print("=");
        }

        System.out.println("


EMPLOYEES & MEMBERS Name and cpr");
        System.out.printf("%-12s", "Name");
        System.out.printf("%-14s", "CPR");
        System.out.println();

        for (int i = 0; i < 62; i++) {
            System.out.print("*");
        }

        udskrivArray2(allEmployeesArray);
        udskrivArray2(allMembersArray);

        System.out.println("
");

        System.out.println();

        String userName1 = "Faisal";

        Scanner login = new Scanner(System.in);
        System.out.println("Please type in your username:");

        String userName = login.nextLine();
        if (userName.equals(userName1)) {
            System.out.println("Welcome " + userName);
        } else {
            System.out.println("Incorrect User - limited choices available");
        }

        String userChoice; // variable som opbevarer det indtastede

        Scanner input = new Scanner(System.in);

        // loop menuen indtil brugeren vælger at afslutte
        while (true) {
            // Vis valgmulighederne
            System.out.println("----Available Choices----");
            System.out.println(" Press 1 for search for a specific employee or member");
            System.out.println(" Press 2 to list and count all employees (CPR)");
            System.out.println(" Press 3 to list the current salaries of all the employees");
            System.out.println(" Press 4 to exit");

            System.out.println("
Please choose from the list:
");

            userChoice = input.next();

            String answer = "";

            switch (userChoice) {
            case "1":

                System.out.println("Please write the name or CPR of the person you are searching for.");
                answer = input.next();

                System.out.println("Searching for " + answer);
                boolean found=false;
                String[] employeeFields = null;
                for (int i = 0; i < allEmployeesArray.length; i++) {
                    System.out.println();
                    if (allEmployeesArray[i][0].equals(answer)) {
                        found=true;
                        employeeFields=allEmployeesArray[i];
                        break;
                    }
                }

                if(found)
                    System.out.println(Arrays.toString(employeeFields));
                else 
                    System.out.println("Person not found");

                System.out.println();
                System.out.println("done with job number 1");
                break;

            case "2":

         

以上是关于在数组(或ArrayList)中搜索由扫描仪用户指定的特定人(值),如果找到,则将该数组中的所有值返回/打印到扫描仪上的主要内容,如果未能解决你的问题,请参考以下文章

在空arraylist中存储用户输入

Arraylist.contains 不会检查字符串

如果其他ArrayList中的条件为true,如何从ArrayList中删除项目

如何使用 curl 在弹性搜索中删除 arraylist 值?

如何在android中将arraylist或vector插入SQLite数据库?

int[]里数的个数怎么由输入决定?-----动态数组。