简体   繁体   中英

How to read a particular row from excel in Java?

I am trying to code a program that allows me to retrieve the details of a person stored in an excel file. I have decided to use emails as a way of identifying each person as these are unique. My program does not appear to work, can someone help me?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Reader {

    public static void main(String[] args) {

        String csvFile = "Clients.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
                if(((br.readLine().split(cvsSplitBy))[2]).equals("email@gmail.com")){
                    String[] data = line.split(cvsSplitBy);

                    System.out.println("First Name: "+data[0]+" Last Name: "+data[1]+" Activity Level: "+data[7]);

                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

Here is my corrected code that fixed my problem:

        String csvFile = "Clients.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
                String[] data = line.split(cvsSplitBy);
                if(data[2].equals("samuelfairbrass@icloud.com")){


                    System.out.println("First Name: "+data[0]+" Last Name: "+data[1]+" Email: "+data[2]+" Phone Number: "+data[3]+" Activity Level: "+data[7]);

                }


            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

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