Search Through File with OPENCSV
All,
I am searching thru a .csv file for using regular expressions. I am looking for a patter that looks like a date: mm/dd/yyyy.
I search for the pattern while reading my .csv file. Since I am in a loop, I can't get a permanent handle on the search result. How can I get a permanent handle on it?
When I come out the loop, the date is empty or null.
How can I get the one occurrence, and save it?
Code:
package mybeans;
import org.apache.regexp.*;
import java.util.LinkedList;
import ClientDataProc.Logging;
import ClientDataProc.BalancesDataClass;
import ClientDataProc.ControlsAccess;
import au.com.bytecode.opencsv.CSVReader;
import java.util.List;
import java.io.*;
import java.io.FileReader;
import java.io.IOException;
public class CSVBalances {
static String find="";
//lets get headers from config file.
String[] headers;
String finalDate ="";
static String date ="";
private static Logging log = new Logging();
public static void main(String[] args) throws IOException {
CSVBalances test = new CSVBalances();
test.readData("Cash.csv", "royal.csv");
System.out.println("From main" +date);
}
public LinkedList<BalancesDataClass> readData(String filename, String config)throws IOException {
ControlsAccess controls = new ControlsAccess(log);
BalancesDataClass balances = new BalancesDataClass(controls, filename, -1,log);
LinkedList<BalancesDataClass> list = new LinkedList<BalancesDataClass>();
//lets get the date out of the file.
CSVReader readerDate = new CSVReader(new FileReader(filename));
String [] nextLineDate;
while((nextLineDate = readerDate.readNext()) != null){
for(int i = 0;i<nextLineDate.length;i++){
finalDate = findDate(nextLineDate);
if(finalDate != null || finalDate != ""){
date = finalDate;
System.out.println(date);
break;
}
break;
}
}
//now we read the data file.
CSVReader reader = new CSVReader(new FileReader(filename),',','\"',3);
String [] nextLine;
//read the configuration file.
File configFile = new File(config);
CSVReader configReader = new CSVReader(new FileReader(configFile));
while((headers = configReader.readNext()) != null){
//lets find the date in the file:
while ((nextLine = reader.readNext()) != null) {
for(int h =0; h<headers.length;h++){
System.out.println(headers[h] + nextLine[h]);
//add to our objects BalancesDataClass/ClientDataClass.
balances.saveData(headers[h], nextLine[h]);
}
}
}return list;
}
private static String findDate(String part){
String date="";
String pattern = "[0-9][0-9]" + "/" + "[0-9][0-9]" + "/" + "[0-9][0-9]";
RE r = new RE(pattern);
if(r.match(part)){
//System.out.println("The match is: " + r.getParen(0));
date = r.getParen(0);
}
return date;
}
public String getDate(){
return this.date;
}
}