【问题标题】:Java - Read and search CSV file and display resultsJava - 读取和搜索 CSV 文件并显示结果
【发布时间】:2020-01-15 06:46:01
【问题描述】:

我正在编写一个程序来显示从 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

非常感谢任何帮助。

【问题讨论】:

  • 您正在阅读行,并将您阅读的内容写入输出流。 Insead,将您阅读的内容存储在 List 中。然后,当用户输入 en 员工 ID 时,循环遍历列表以找到它。

标签: java arrays csv


【解决方案1】:

首先创建一个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

【讨论】:

    【解决方案2】:

    您应该为此使用 hashtable 或任何其他类似的类。创建一个名为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/

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      相关资源
      最近更新 更多