简体   繁体   中英

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. I then wish to be able to select an employee based off there ID number and display information about them. The user will enter an Employee ID number and search the CSV file to display that employees information.

NOTE: i don't wish to use a CSV library as i'm still learning the fundamentals.

CSV file contents are: (the headers are not in the file, just for understanding the info)

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:

 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.

after that inside your above code create instance of arraylist of objects of your employee class like this.

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

Now inside your while loop add new employee details into your list object.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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