繁体   English   中英

Java - 读取和搜索 CSV 文件并显示结果

[英]Java - Read and search CSV file and display results

我正在编写一个程序来显示从 CSV 文件中读取的员工列表。 然后我希望能够根据 ID 号选择一名员工并显示有关他们的信息。 用户将输入员工 ID 号并搜索 CSV 文件以显示该员工信息。

注意:我不想使用 CSV 库,因为我仍在学习基础知识。

CSV 文件内容是:(标题不在文件中,只是为了了解信息)

EmpID,Name,Age,Position,Type,Rate
    1,Jason Thomas-Junior,27,Sales,Full Time,150
    2,Tim Green,25,Sales,Full Time,59
    3,Tony Watson,25,Sales,Casual,55
    4,Geoff Hart,27,Sales,Casual,110
    5,Fred Mercury,35,Supervisor,Full Time,60

我能够以以下格式显示信息:

____________________________________________________________________
EmpID    Name                  Age     Position      Type      Rate/Day

1        Jason Thomas-Junior   27       Sales        Full Time   150.0   
2        Tim Green             25       Sales        Full Time   59.0    
3        Tony Watson           25       Sales        Caual       55.0    
4        Geoff Hart            27       Sales        Casual      110.0   
5        Fred Mercury          35       Supervisor   Full Time   60.0    
____________________________________________________________________

我显示上面输出的代码是:

try {
            Scanner fileScanner = new Scanner(new FileReader("EmpList.csv"));
            fileScanner.useDelimiter(",");

            while (fileScanner.hasNextLine()) {
                fileName = fileScanner.nextLine();
                String[] data = fileName.split(",");
                int empID = Integer.parseInt(data[0]);
                String empName = data[1];
                int empAge= Integer.parseInt(data[2]);
                String empPosition = data[3];
                String empType = data[4];
                double empRate= Double.parseDouble(data[5]);


                System.out.printf("%-9s%-16s%-8s%-14s%-10s%-8s\n"
                        , empID , empName , empAge, empPosition , empType , empRate);

            }
            fileScanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

我现在被困在如何搜索 EmpID 并以以下格式显示汇总结果:

 System.out.println("Select the employee number from the list: ");

 Employee is:         name
 Employees rate is:   rate

任何帮助是极大的赞赏。

首先创建一个 Employee 类,然后创建实例变量 ID、名称、年龄、职位、类型、比率,并创建 setter 和 getter 方法。

之后在上面的代码中创建像这样的员工类对象的arraylist实例。

ArrayList<Employee> employee = new ArrayList<>();

现在在您的 while 循环中将新员工详细信息添加到您的列表对象中。

employee.add(new Employee(X,XX,XX,XX,XX));

一旦您从文件中读取所有记录,您就可以对其执行搜索功能。

对于搜索,您可以对列表进行迭代。

//for validation
boolean flag = false;
    for(Employee emp:employee){

        if(emp.getID() == 101){
         //enter print code here
          flag = true;
    }            
    }

    if(!flag) //print no record found

您应该为此使用哈希表或任何其他类似的类。 创建一个名为employee 的类。

Class Employee
{
//declare employee fields on here

}

//declare hash table to input employee details
 Hashtable<Integer,Employee> EmployeeTable=new Hashtable<Integer,Employee>();  
//accept input as employee object in your while loop
while (fileScanner.hasNextLine()) {
                fileName = fileScanner.nextLine();
                String[] data = fileName.split(",");
Employee obj=new Employee()//
//write code to input values to employee
EmployeeTable.put(obj.ID,obj); //put employee id as key and employee as data

}

/* 使用员工 ID 键获取值 */ EmployeeTable.get(employee_id);//

请参阅https://www.geeksforgeeks.org/hashtable-get-method-in-java/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM