Home > Software engineering >  Searching a line by date in a CSV file
Searching a line by date in a CSV file

Time:05-17

So, i have this CSV file and i need to do a program that can search through the CSV with the user inserting a date (all the dates are values in the csv). Honestly i'm completely lost on what to do, and if anyone of you could help me it would be great! CSV line example:

6288,Ossidi di Azoto,µg/m³,551,Arconate v. DeGasperi,182,MI,Arconate,20/01/1999

this is the code i've done so far using examples online but i don't think anything of it is near to being right

public String ricercaCsv(int CercaIndice, String CercaStringa, String CercaData) throws IOException {
        String RigaRisultato = null;
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\leona\\IdeaProjects\\PrjAria\\src\\Aria.csv"));
        String linea;
        while ( (linea = br.readLine()) != null ) {
            String[] values = linea.split(",");
            if(values[CercaIndice].equals(CercaStringa)) {
                RigaRisultato = linea;
                break;
            }
        }
        br.close();
        return RigaRisultato;


    }

CodePudding user response:

Mykong answered in his article.

Part 1:

My personal approach will be to use the external library if you can:

  try (CSVReader reader = new CSVReader(new FileReader("file.csv"))) {
      String[] lineInArray;
      while ((lineInArray = reader.readNext()) != null) {
          System.out.println(lineInArray[0]   lineInArray[1]   "etc...");
      }
  }

with

<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>5.6</version>
</dependency>

If you use something different from Maven, see syntax for your project builder on Maven.

Part 2: Ideally, when you know that you are looking for the date 10/10/1999, I recommend transform these dates (it should be assumingly the same column all the time).

Possible example,

String date = "10/10/1999";
SimpleDateFormat sdt = new SimpleDateFormat("dd/MM/YYYY");
Date result = sdt.parse(date);

CodePudding user response:

If I get it right your csv lines end with the date column, and you are searching for a line which ends with the date enterd by the user. So you don't need to split the lines at all and just use Strings.endsWith() for each line to check for a match.

So the easiest solution would be to use the methods from the java.nio package to read a file and filter the line you need. Here are three ways:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

 ....

public String ricercaCsv_1(String CercaData) throws IOException {
    final String fileName = "C:\\Users\\leona\\IdeaProjects\\PrjAria\\src\\Aria.csv";
    final Stream<String> fileContent = Files.lines(Paths.get(fileName));
    return fileContent.filter(line -> line.endsWith(CercaData)).findFirst().orElse(null);
}

example usage to search by name for the above method:

String result = ricercaCsv_1("19/05/2022");

If you want to stick with your approach using BufferedReader

public String ricercaCsv_2(String CercaData) throws IOException {
    String RigaRisultato = null;
    BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\leona\\IdeaProjects\\PrjAria\\src\\Aria.csv"));
    String linea;
    while ( (linea = br.readLine()) != null ) {
        if(linea.endsWith(CercaData)) {
            RigaRisultato = linea;
            break;
        }
    }
    br.close();
    return RigaRisultato;
}

example usage to search by name for the above method:

String result = ricercaCsv_2(8, "19/05/2022");

If you want to reuse your method and be able to search by any column by just passing the column index, something like below should work:

public String ricercaCsv_3(int CercaIndice, String CercaStringa) throws IOException {
    String RigaRisultato = null;
    BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\leona\\IdeaProjects\\PrjAria\\src\\Aria.csv"));
    String linea;
    while ( (linea = br.readLine()) != null ) {
        String[] values = linea.split(",");
        if(values[CercaIndice].equals(CercaStringa)) {
            RigaRisultato = linea;
            break;
        }
    }
    br.close();
    return RigaRisultato;
}

example usage to search by name for the above method:

String result = ricercaCsv_3(8, "19/05/2022");
  • Related