简体   繁体   English

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

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

I am writing a program to display a list of employees read from a CSV file.我正在编写一个程序来显示从 CSV 文件中读取的员工列表。 I then wish to be able to select an employee based off there ID number and display information about them.然后我希望能够根据 ID 号选择一名员工并显示有关他们的信息。 The user will enter an Employee ID number and search the CSV file to display that employees information.用户将输入员工 ID 号并搜索 CSV 文件以显示该员工信息。

NOTE: i don't wish to use a CSV library as i'm still learning the fundamentals.注意:我不想使用 CSV 库,因为我仍在学习基础知识。

CSV file contents are: (the headers are not in the file, just for understanding the info) 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

I am able to display the info in the following format:我能够以以下格式显示信息:

____________________________________________________________________
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    
____________________________________________________________________

My code to display the output above is:我显示上面输出的代码是:

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();
        }

I am now stuck on how to search an for a EmpID and display a summarised result in the following format:我现在被困在如何搜索 EmpID 并以以下格式显示汇总结果:

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

 Employee is:         name
 Employees rate is:   rate

Any help is greatly appreciated.任何帮助是极大的赞赏。

First of all create a class Employee then create instance variables ID, name, age, position, type, rate and also create setter and getter methods.首先创建一个 Employee 类,然后创建实例变量 ID、名称、年龄、职位、类型、比率,并创建 setter 和 getter 方法。

after that inside your above code create instance of arraylist of objects of your employee class like this.之后在上面的代码中创建像这样的员工类对象的arraylist实例。

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

Now inside your while loop add new employee details into your list object.现在在您的 while 循环中将新员工详细信息添加到您的列表对象中。

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

Once you read all records from file now you can perform searching function over it.一旦您从文件中读取所有记录,您就可以对其执行搜索功能。

for searching you can do iteration over list.对于搜索,您可以对列表进行迭代。

//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

You should use hashtable or any other simlar class for that.您应该为此使用哈希表或任何其他类似的类。 Create a class named employee.创建一个名为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

}

/* get values by using employee id key */ EmployeeTable.get(employee_id);// /* 使用员工 ID 键获取值 */ EmployeeTable.get(employee_id);//

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

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

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