search range data on CSV file using java - java

I'm trying to get selective data from .csv file using java.
for example:
CSV file contains:
Blue, 03/11/2014, 13:00, 10
pink, 04/11/2014, 14:00, 15
Red, 03/11/2014, 15:00, 50
I want to create a program in java which will allow users to select what info they want from that file.
I've been working on the example below but only able to print strings and not the dates/intergers:
package csv;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.Scanner;
public class ReadCVS {
public static void main(String[] args) {
ReadCVS obj = new ReadCVS();
obj.run();
}
public void run() {
String csvFile = "GeoIPCountryWhois.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
File file = new File("GeoIPCountryWhois.csv");
Scanner in = null;
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] colour = line.split(cvsSplitBy);
//System.out.println(country[0]+ country[1] + country[2]+ country[3]);
for (int i = 0; i < colour.length; i++) {
if (colour[i].equals("Pressure")) {
System.out.println(colour[0] + colour[1] + colour[2] + colour[3]); //Matching the string and printing.
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
Any help/tips will be appreciated!
thanks

I would recommend using 3rd party library for parsing the csv file. This lets you focus on the essence of what you are trying to do, instead of getting hung up on file parsing. Have a look at, for example Apache Commons CSV. This would let your code look like this:
Reader in = new FileReader("GeoIPCountryWhois.csv");
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
String colour = record.get(0);
Date date = df.parse(record.get(1));
String timeString = record.get(2);
Integer value = Integer.parseInt(record.get(3));
// do what you want with the values here.
}
Notice how this does some additional parsing, such as parsing the Date and the Integer. This will let you more easily filter those columns because you can do comparisons.
If you don't want third party dependencies, you could do something similar to what you have:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
while ((line = br.readLine()) != null) {
// use comma as separator
String[] columns = line.split(cvsSplitBy);
// extract the columns.
String colour = columns[0].trim();
Date date = df.parse(columns[1].trim());
String time = columns[2].trim();
Integer otherValue = Integer.parseInt(columns[3].trim());
// filter on the colour column.
if(colour.equals("Red")) {
System.out.printf("colour = %s, date = %s, time = %s, val = %d\n", colour, df.format(date), time, otherValue);
}
}
Note how this code is calling String.trim() on all of the columns. This is in case there is extra whitespace around the column after splitting the line. For example, "a, b, c".split(",") would result in the String array {"a", " b", " c"} which has an extra space in " b" and " c". This is probably the source of the bug you have where you can only filter on the first column.
As a third option, which is sort of overkill, you could use CsvJdbc. This provides a JDBC interface to sql files, which you can then execute SQL queries over. I've never used this library but it looks interesting. Given that you are trying to filter CSV files.

Related

Easiest way to grab specific 'elements' from a string array of imported spreadsheet data?

Sorry for the awkward wording of the question. Long story short, I have an excel spreadsheet of 10 different areas of New York and their respective weekly gas price averages from 2008-2018. My assignment is to extract Buffalo's average gas price per year. The data from the entire spreadsheet is put into a string array, and the current approach I'm taking is most definitely wrong.
How would I go about grabbing say, every weekly gas price for buffalo in 2018? If anyone could give me a tip on a head start with that, I'd be able to do the rest of the years, as well as the other two portions of my homework assignment. I believe I am supposed to use StringBuilder, as that's what we're covering in class currently. (Specifically learning split, append, and substring through StringBuilder)
Here's my current code:
package Chapter9;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
public class GasPrices {
public static void main(String[] args) throws IOException {
String[] gasData = getData(); // download gas price into an array
StringBuilder sb = new StringBuilder();
for(String s : gasData) {
System.out.println(s);
sb.append(s);
}
}
public static String[] getData() throws IOException {
URL url = new URL("https://cs.wcupa.edu/schen/csc240/Gasoline_Retail_Prices_Weekly_Average_by_Region__Beginning_2008.csv");
URLConnection connection = url.openConnection();
InputStreamReader input = new InputStreamReader(connection.getInputStream());
BufferedReader buffer = null;
StringBuilder data = new StringBuilder();
String line = "";
try {
buffer = new BufferedReader(input);
while ((line = buffer.readLine()) != null) {
data.append(line + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data.toString().split("\n");
}
}
And my output looks like this: (Only put the data for 2017-2018)
Date,New York State Average,Albany Average,Binghamton Average,Buffalo Average,Nassau Average,New York City Average,Rochester Average,Syracuse Average,Utica Average
12/31/18,2.64,2.51,2.55,2.7,2.58,2.7,2.61,2.52,2.62
12/24/18,2.69,2.56,2.61,2.75,2.63,2.75,2.66,2.57,2.67
12/17/18,2.72,2.59,2.65,2.8,2.67,2.78,2.69,2.6,2.71
12/10/18,2.76,2.62,2.69,2.86,2.7,2.81,2.74,2.64,2.75
12/3/18,2.8,2.67,2.75,2.9,2.75,2.86,2.78,2.68,2.78
11/26/18,2.85,2.72,2.8,2.93,2.79,2.91,2.82,2.72,2.82
11/19/18,2.88,2.75,2.84,2.95,2.83,2.94,2.85,2.77,2.85
11/12/18,2.92,2.8,2.89,2.97,2.88,2.98,2.89,2.82,2.89
11/5/18,2.95,2.83,2.91,2.98,2.93,3.03,2.91,2.85,2.91
10/29/18,2.97,2.85,2.93,2.99,2.96,3.06,2.93,2.89,2.92
10/22/18,2.99,2.87,2.94,2.99,2.98,3.09,2.95,2.91,2.94
10/15/18,3.01,2.89,2.96,3,2.99,3.1,2.97,2.94,2.97
10/8/18,3,2.9,2.95,2.99,2.99,3.1,2.97,2.94,2.97
10/1/18,2.99,2.86,2.91,2.99,2.97,3.09,2.93,2.91,2.94
9/24/18,2.99,2.87,2.92,2.99,2.98,3.1,2.94,2.92,2.93
9/17/18,3,2.88,2.93,2.99,2.99,3.1,2.96,2.93,2.94
9/10/18,3,2.89,2.93,2.99,2.98,3.09,2.95,2.93,2.95
9/3/18,2.99,2.87,2.92,2.99,2.98,3.09,2.92,2.89,2.94
8/27/18,2.99,2.87,2.92,2.99,2.99,3.1,2.93,2.9,2.95
8/20/18,3,2.88,2.94,2.99,3,3.11,2.94,2.92,2.97
8/13/18,3.01,2.89,2.95,3,3.01,3.1,2.96,2.93,2.97
8/6/18,3.01,2.9,2.96,3,3.01,3.1,2.97,2.93,2.97
7/30/18,3,2.88,2.92,3,3,3.1,2.95,2.9,2.96
7/23/18,3.01,2.89,2.93,3,3.01,3.1,2.95,2.91,2.97
7/16/18,3.02,2.91,2.95,3,3.03,3.11,2.97,2.92,2.98
7/9/18,3.02,2.92,2.97,3,3.03,3.12,2.98,2.92,2.98
7/2/18,3.02,2.92,2.94,3,3.02,3.11,2.96,2.9,2.98
6/25/18,3.03,2.94,2.96,3.01,3.04,3.13,2.98,2.93,3.01
6/18/18,3.06,2.97,2.98,3.01,3.07,3.16,3,2.98,3.04
6/11/18,3.08,2.99,3,3.02,3.1,3.19,3.02,3.01,3.07
6/4/18,3.09,3.01,3.01,3.01,3.12,3.21,3.03,3.04,3.08
5/28/18,3.09,3.01,3.02,2.99,3.12,3.2,3.03,3.05,3.09
5/21/18,3.04,2.94,3,2.94,3.07,3.14,2.98,3.01,3.04
5/14/18,2.98,2.87,2.94,2.89,3.01,3.08,2.91,2.95,2.97
5/7/18,2.95,2.84,2.92,2.86,2.99,3.05,2.88,2.92,2.94
4/30/18,2.92,2.82,2.91,2.81,2.96,3.02,2.87,2.9,2.91
4/23/18,2.85,2.76,2.84,2.77,2.88,2.94,2.83,2.83,2.84
4/16/18,2.79,2.7,2.77,2.73,2.81,2.88,2.73,2.74,2.76
4/9/18,2.77,2.68,2.74,2.71,2.79,2.86,2.71,2.71,2.74
4/2/18,2.76,2.65,2.74,2.71,2.78,2.85,2.71,2.71,2.73
3/26/18,2.71,2.61,2.7,2.68,2.71,2.79,2.67,2.66,2.7
3/19/18,2.69,2.6,2.68,2.66,2.68,2.76,2.63,2.61,2.7
3/12/18,2.7,2.61,2.69,2.67,2.69,2.77,2.65,2.62,2.71
3/5/18,2.71,2.62,2.7,2.67,2.71,2.79,2.67,2.64,2.72
2/26/18,2.73,2.64,2.7,2.68,2.73,2.8,2.68,2.66,2.73
2/19/18,2.75,2.66,2.72,2.68,2.76,2.82,2.69,2.69,2.75
2/12/18,2.77,2.68,2.75,2.69,2.78,2.85,2.71,2.72,2.76
2/5/18,2.76,2.68,2.74,2.68,2.79,2.85,2.7,2.72,2.76
1/29/18,2.73,2.64,2.71,2.66,2.75,2.81,2.67,2.68,2.72
1/22/18,2.69,2.6,2.65,2.64,2.69,2.78,2.63,2.62,2.66
1/15/18,2.67,2.59,2.63,2.63,2.65,2.75,2.62,2.61,2.65
1/8/18,2.66,2.58,2.62,2.62,2.63,2.74,2.59,2.59,2.64
1/1/18,2.63,2.53,2.59,2.62,2.6,2.72,2.55,2.53,2.6
12/25/17,2.62,2.52,2.58,2.63,2.6,2.72,2.54,2.52,2.58
12/18/17,2.64,2.54,2.6,2.64,2.61,2.73,2.55,2.53,2.6
12/11/17,2.66,2.57,2.62,2.65,2.63,2.75,2.58,2.56,2.62
12/4/17,2.68,2.6,2.63,2.65,2.66,2.77,2.6,2.58,2.63
11/27/17,2.68,2.61,2.64,2.65,2.67,2.78,2.61,2.59,2.64
11/20/17,2.7,2.63,2.65,2.66,2.68,2.79,2.64,2.6,2.65
11/13/17,2.69,2.63,2.64,2.65,2.67,2.8,2.64,2.6,2.64
11/6/17,2.65,2.59,2.6,2.64,2.62,2.77,2.6,2.54,2.6
10/30/17,2.64,2.58,2.59,2.65,2.6,2.77,2.56,2.48,2.59
10/23/17,2.65,2.6,2.61,2.67,2.62,2.78,2.56,2.46,2.62
10/16/17,2.68,2.62,2.64,2.69,2.66,2.81,2.61,2.5,2.66
10/9/17,2.72,2.65,2.68,2.71,2.71,2.85,2.66,2.56,2.68
10/2/17,2.76,2.67,2.71,2.73,2.76,2.88,2.71,2.64,2.72
9/25/17,2.79,2.7,2.74,2.75,2.81,2.93,2.75,2.7,2.75
9/18/17,2.82,2.72,2.76,2.76,2.85,2.96,2.76,2.75,2.77
9/11/17,2.82,2.72,2.76,2.73,2.87,2.97,2.74,2.74,2.78
9/4/17,2.58,2.48,2.53,2.51,2.63,2.72,2.51,2.49,2.53
8/28/17,2.48,2.34,2.43,2.43,2.51,2.61,2.41,2.36,2.42
8/21/17,2.48,2.35,2.43,2.42,2.52,2.61,2.42,2.38,2.42
8/14/17,2.48,2.36,2.43,2.38,2.53,2.62,2.42,2.39,2.43
8/7/17,2.47,2.35,2.42,2.37,2.52,2.61,2.4,2.38,2.41
7/31/17,2.44,2.32,2.38,2.35,2.48,2.59,2.36,2.33,2.38
7/24/17,2.42,2.3,2.36,2.34,2.45,2.58,2.35,2.3,2.37
7/17/17,2.41,2.28,2.36,2.34,2.44,2.57,2.34,2.27,2.36
7/10/17,2.42,2.29,2.36,2.34,2.44,2.58,2.34,2.28,2.37
7/3/17,2.43,2.3,2.38,2.35,2.46,2.59,2.36,2.29,2.38
6/26/17,2.45,2.32,2.39,2.37,2.47,2.6,2.38,2.31,2.4
6/19/17,2.48,2.36,2.42,2.41,2.5,2.62,2.41,2.35,2.42
6/12/17,2.5,2.39,2.44,2.42,2.52,2.63,2.43,2.38,2.44
6/5/17,2.51,2.42,2.47,2.43,2.53,2.64,2.45,2.41,2.46
5/29/17,2.51,2.43,2.47,2.43,2.53,2.64,2.44,2.4,2.45
5/22/17,2.5,2.42,2.45,2.43,2.52,2.63,2.42,2.38,2.45
5/15/17,2.51,2.43,2.46,2.45,2.53,2.63,2.43,2.4,2.46
5/8/17,2.52,2.44,2.48,2.47,2.55,2.64,2.45,2.43,2.48
5/1/17,2.54,2.46,2.5,2.49,2.56,2.65,2.47,2.46,2.5
4/24/17,2.53,2.47,2.51,2.49,2.54,2.64,2.47,2.51,2.5
4/17/17,2.51,2.44,2.49,2.48,2.51,2.62,2.45,2.44,2.48
4/10/17,2.47,2.37,2.41,2.43,2.47,2.6,2.39,2.37,2.42
4/3/17,2.43,2.31,2.37,2.4,2.42,2.58,2.34,2.3,2.37
3/27/17,2.42,2.31,2.37,2.4,2.42,2.58,2.34,2.29,2.36
3/20/17,2.43,2.31,2.37,2.41,2.44,2.59,2.35,2.3,2.37
3/13/17,2.44,2.31,2.38,2.41,2.45,2.59,2.35,2.31,2.38
3/6/17,2.45,2.32,2.39,2.42,2.47,2.61,2.36,2.33,2.39
2/27/17,2.47,2.34,2.4,2.43,2.49,2.62,2.37,2.34,2.41
2/20/17,2.48,2.36,2.41,2.44,2.51,2.63,2.39,2.35,2.41
2/13/17,2.48,2.36,2.41,2.44,2.51,2.64,2.4,2.33,2.41
2/6/17,2.5,2.38,2.43,2.46,2.52,2.65,2.42,2.36,2.42
1/30/17,2.51,2.4,2.46,2.46,2.54,2.66,2.45,2.38,2.45
1/23/17,2.53,2.43,2.48,2.47,2.56,2.66,2.47,2.42,2.46
1/16/17,2.54,2.45,2.5,2.47,2.57,2.66,2.48,2.44,2.48
1/9/17,2.54,2.46,2.51,2.46,2.57,2.66,2.48,2.44,2.49
1/2/17,2.49,2.41,2.46,2.42,2.53,2.62,2.43,2.4,2.46
You can store all the lines into a double dimensional array, say data:
StringBuilder sb = new StringBuilder(input);
int rows = sb.toString().split("\n").length;
int cols = sb.toString().split("\n").split(",").length;
String[] data = new String[rows][cols];
Now, each row of data stores the values for a particular date, with the date being at index 0 i.e. data[0]
To access the data for Buffalo for each date, make a loop that goes through each row of data, and extract the element at index 4 for each row i.e. data[i][4]:
for(int i = 0; i < rows; i++)
{
System.out.println("The date is: "+data[i][0]);
System.out.println("Buffalo's average is: "+data[i][4]);
}

Is there any in built function in Java to remove unwanted data from extracted data

I extracted some text from a text file but now I want only some specific words from that text.
What I have tried is read from that text file and I have searched by using keyword:
FileReader fr = new
FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
BufferedReader br = new BufferedReader(fr);
String s;
String keyword = "dba COPIEFacture ";
while ((s = br.readLine()) != null) {
if (s.contains(keyword)) {
System.out.println(s);
I got Output like this: dba COPIEFacture du 28/05/2018 n° 10077586115Récapitulatif de vote facture
But I want only 28/05/2018 This so please help me
You'll need to use String manipulation methods.
It's difficult to know the best way to do it without seeing other outputs, but you could probably use split() and indexOf() to retrieve the date.
There are other, probably more complex, methods. For example, here's a StackOverflow answer about retrieving dates from strings using a regex pattern.
This will do the trick.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
FileReader fr;
String keyword = "dba COPIEFacture du ";
String textToFind = "28/05/2018"; // The length usually will not
// change.You can use value
// 10(length) instead
StringBuilder sb = new StringBuilder();
try {
fr = new FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
int i;
while ((i = fr.read()) != -1) {
sb.append((char) i);
}
int start = sb.indexOf(keyword) + keyword.length();
int end = start + textToFind.length();
System.out.print(sb.substring(start, end)); //output: 28/05/2018
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Using a loop I want to find the value of a column from my CSV file java

Forgive me if this is a basic (or not very well explained) question, I am fairly new to Java and have been reading extensive material as well as trying to understand the relevant Javadoc but to no avail.
To give a brief background as to what I am trying to create, I have created a reader class which reads data in from a csv file (4 lines long) including fields such as Item ID, price, description etc. I have created a separate demo class that displays the details of this csv file (through creating an instance of my reader class) and am now trying to create a method that asks the user to input an Item ID that then displays the corresponding Item, based on the ID input by the user. The part I am stuck on is accessing specific rows/columns in a csv file and then comparing these with a given string (entered by the user which corresponds to a specific field in the csv file)
This is what I have come up with thus far:
input = new Scanner(System.in);
System.out.println("Enter a product code");
String prodC = input.next();
//Here I want to know if there is a way of accessing a field in a csv file
Any ideas would be greatly appreciated.
UPDATE
Thank you for quick responses, am currently reading through and seeing how I can try to implement the various techniques. In response to the comment asking about the file reader, this is how I have set that out:
public CatalogueReader(String filename) throws FileNotFoundException {
this.filename = filename;
this.catalogue = new Catalogue();
Scanner csvFile;
try {
csvFile = new Scanner(new File(filename));
} catch (FileNotFoundException fnf) {
throw new FileNotFoundException("File has not been found!");
}
csvFile.useDelimiter("\n");
boolean first = true;
String productCode;
double price;
String description;
double weight;
int rating;
String category;
boolean ageRestriction;
String csvRows;
while (csvFile.hasNextLine()) {
csvRows = csvFile.nextLine();
if (first) {
first = false;
continue;
}
System.out.println(csvRows);
String[] fields = csvRows.split(",");
productCode = (fields[0].trim());
price = Double.parseDouble(fields[1].trim());
description = fields[2].trim();
weight = Double.parseDouble(fields[3].trim());
rating = Integer.parseInt(fields[4].trim());
category = fields[5].trim();
ageRestriction = Boolean.parseBoolean(fields[6].trim());
catalogue.addAProduct(new Item(productCode, price, description, weight, rating, category, ageRestriction));
}
csvFile.close();
}
}
ok so for a CSV file like this:
"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"
here is a sample of how to read using Java Native Libraries
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CSVReader {
public static void main(String[] args) {
CSVReader obj = new CSVReader();
obj.run();
}
public void run() {
String csvFile = YOURFILEPATHHERE ;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
System.out.println("Country [code= " + country[4]
+ " , name=" + country[5] + "]");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
does this help?
If you are just doing a single look-up and then exiting then just remember the String you are looking for. As you parse the lines compare to see if you have a match and if you do then return that line.
For repeated searches that would be very inefficient though. Assuming your data set is not too large for memory you would be better off parsing the file and putting it into a Map:
Map<String, Data> dataMap = new HashMap<>();
Parse the file, putting all the lines into the map
Then the lookup just becomes:
Data d = dataMap.get(lineKey);
If d is null then there is no matching line. If it not null then you have found your line.
You can create an array list of object. An object for each line in the CSV. Then search the array object with your search criteria.
User CSVReader framework to read the csv file. Sample code (not exactly what you want)
FileInputStream fis = new FileInputStream(file);
CSVReader reader = new CSVReader(new BufferedReader( new InputStreamReader(fis, "UTF-8" )));
ArrayList<String> row = new ArrayList<String>();
ArrayList<Entry> entries = new ArrayList<Entry>();
// a line = ID, Name, Price, Description
while (!reader.isEOF()) {
reader.readFields(row);
if( row.size() >= 4)
entries.add(new Entry(row.get(0), row.get(1), row.get(2), row.get(3)));
}
System.out.println("Size : "+entries);

Comparing two lines in text file & printing single line corresponding to similar dates

I want to take line 1 and line 5 which has username and date same but in line one it contains in time,and in line 5 it contain out time
I want to read those two lines and compare it to check whether both lines have same username and date and if so print it as single line in some other text file or in hash map
example like this : "sangeetha-May 02, 2013 , -in-09:48:06:61 -out-08:08:19:27 (in JAVA)
This is the content of text file :
line 1. "sangeetha-May 02, 2013 , -in-09:48:06:61
line 2. "lohith-May 01, 2013 , -out-09:10:41:61
line 3 . "sushma-May 02, 2013 , -in-09:48:06:61
line 4. "sangeetha-May 01, 2013 , -out-08:36:38:50
line 5. "sangeetha-May 02, 2013 , -out-08:08:19:27
line 6. "sushma-May 02, 2013 , -out-07:52:13:51
line 7. "sangeetha-Jan 01, 2013 , -in-09:27:17:52-out-06:47:48:00
line 8. "madhusudhan-Jan 01, 2013 , -in-09:38:59:31-out-07:41:06:40
Above data is generating by using code below
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;
public class FlatFileParser
{
public static void main(String[] args)
{
// The stream we're reading from
BufferedReader in;
BufferedWriter out1;
BufferedWriter out2;
// Return value of next call to next()
String nextline;
try
{
if (args[0].equals("1"))
{
in = new BufferedReader(new FileReader(args[1]));
nextline = in.readLine();
while(nextline != null)
{
nextline = nextline.replaceAll("\\<packet","\n<packet");
System.out.println(nextline);
nextline = in.readLine();
}
in.close();
}
else
{
in = new BufferedReader(new FileReader(args[1]));
out1 = new BufferedWriter(new FileWriter("inValues.txt" , true));
out2 = new BufferedWriter(new FileWriter("outValues.txt"));
nextline = in.readLine();
HashMap<String,String> inout = new HashMap<String,String>();
while(nextline != null)
{
try
{
if (nextline.indexOf("timetracker")>0)
{
String from = "";
String indate = "";
if (nextline.indexOf("of in")>0)
{
int posfrom = nextline.indexOf("from");
int posnextAt = nextline.indexOf("#", posfrom);
int posts = nextline.indexOf("timestamp");
from = nextline.substring(posfrom+5,posnextAt);
indate = nextline.substring(posts+11, posts+23);
String dd = indate.split(" ")[1];
String key = dd+"-"+from+"-"+indate;
//String key = from+"-"+indate;
String intime = "-in-"+nextline.substring(posts+24, posts+35);
inout.put(key, intime);
}
else if (nextline.indexOf("of out")>0)
{
int posfrom = nextline.indexOf("from");
int posnextAt = nextline.indexOf("#", posfrom);
int posts = nextline.indexOf("timestamp");
from = nextline.substring(posfrom+5,posnextAt);
indate = nextline.substring(posts+11, posts+23);
String dd = indate.split(" ")[1];
String key = dd+"-"+from+"-"+indate;
String outtime = "-out-"+nextline.substring(posts+24, posts+35);
if (inout.containsKey(key))
{
String val = inout.get(key);
if (!(val.indexOf("out")>0))
inout.put(key, val+outtime);
}
else
inout.put(key, outtime);
}
}
}
catch(Exception e)
{
System.err.println(nextline);
System.err.println(e.getMessage());
}
nextline = in.readLine();
}
in.close();
for(String key: inout.keySet())
{
String val = inout.get(key);
out1.write(key+" , "+val+"\n");
System.out.println(key + val);
}
out1.close();
}
}
catch (IOException e)
{
throw new IllegalArgumentException(e);
}
}
}
Description: These are log in and log out times of the employees,i am reading these from a log file ,but some are coming properly in single line like line 7 and line 8
and some are coming in different lines for same date,i want it to print in same line like example i have provided above,
and which ever records coming in single line both in and out time ishould retain it as it is....
PLZ CAN ANYBODY HELP ....!
Considering you have a list of all lines from the file in lstFile.
You can do this
String output="",line1,line2;
for(int i=0;i<lstFile.size();i++)
{
line1=lstFile.get(i);
if(line1.contains("in") && line1.contains("out"))continue;
for(int j=i+1;j<lstFile.size();j++)
{
line2=lstFile.get(j);
if(line2.contains("in") && line2.contains("out"))continue;
if(line1.contains(getNameDate(line2)) && line2.contains("out") && line1.contains("in"))
{
output+=line1+line2.substring(line2.lastIndexOf(","),line2.length());
output+=System.getProperty("line.separator");
break;
}
}
}
//output now contains your desired result
The below method would get the name and date
public String getNameDate(String input)
{
return input.substring(0,input.lastIndexOf(","));
}
This is basic data parsing. In pseudo code this is how I would do it
Create a class that holds 4 valus, Employee, Date, InTime, OutTime
Instantiate a HashMap for all the final log lines
For each log line
Parse the line using RegEx to find Employee, Date and in and/or out time
Create the HashKey using Employee + Date
See if the HashMap already contains such an object, else create one
Populate with the in and/or out times found on the current line
Done, the HashMap now contains all parsed data.
Here's some sample code to get you started.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ParseLogs {
public static void main(String[] args) {
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("src/main/resources/log.txt"));
while ((line = br.readLine()) != null) {
String[] split = line.split(" ");
if (split.length > 2) {
String name = split[2].split("-")[0];
name = name.replace("\"", "");
System.out.println(name);
}
if (split.length > 5) {
String date = split[6];
System.out.println(date);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
The other post is correct that you need to use a regular expression and look at the patterns of the string lines so you can break them up. If possible, try to standardize the logs initially so that they fit a similar pattern and you don't need to massage the data afterward.
Here's the output the program
sangeetha
-in-09:48:06:61
lohith
-out-09:10:41:61
.
,
sangeetha
-out-08:36:38:50
sangeetha
-out-08:08:19:27
sushma
-out-07:52:13:51
sangeetha
-in-09:27:17:52-out-06:47:48:00
madhusudhan
-in-09:38:59:31-out-07:41:06:40
Notice that you still need to clean up the "name" and "date" variables for errors but hopefully that helps.

Best way to process strings in Java

I want make a game of life clone using text files to specify my starting board, then write the finished board to a new text file in a similar format. E.g. load this:
Board in:
wbbbw
bbbbb
wwwww
wbwbw
bwbwb
from a file, then output something like this:
Board out:
wbbbw
bbbbb
wwwww
wwwww
bwbwb
I do this by making a 2D array (char[][] board) of characters, reading the file line by line into a string, and using String.charAt() to access each character and store it in the array.
Afterward, I convert each element of board (i.e., board[0], board[1], etc.), back to a string using String.valueOf(), and write that to a new line in the second file.
Please tell me I'm an idiot for doing this and that there is a better way to go through the file -> string -> array -> string -> file process.
You can use String.toCharArray() for each line while reading the file.
char[][] board = new char[5][];
int i = 0;
while((line = buffRdr.readLine()) != null) {
board[i++] = line.toCharArray();
}
And while writing either String.valueOf() or java.util.Arrays.toString().
for(int i=0; i<board.length; i++) {
//write Arrays.toString(board[i]);
}
// Remember to handle whitespace chars in array
char[] row = "wbbbw bbbbb wwwww wbwbw bwbwb".toCharArray()
Everything else seems good.
Why not use an already existing text format such as JSON instead of inventing your own?
There are tons of JSON parsers out there that can read and write two dimensional arrays.
You get both the benefit of easy reading directly from the file(as with your original method) and the benefit of not having to parse an annoying string format.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class GameOfLife
{
private String mFilename;
private ArrayList<String> mLines;
public GameOfLife(String filename)
{
mFilename = filename;
read();
}
public char get(int x, int y)
{
String line = mLines.get(y);
return line.charAt(x);
}
public void set(char c, int x, int y)
{
String line = mLines.get(y);
String replacement = line.substring(0, x) + c + line.substring(x + 1, line.length());
mLines.set(y, replacement);
}
private void read()
{
mLines = new ArrayList<String>();
try
{
BufferedReader in = new BufferedReader(new FileReader(mFilename));
String line = in.readLine();
while (line != null)
{
mLines.add(line);
line = in.readLine();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void write()
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(mFilename));
for (String line : mLines)
{
out.write(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Categories

Resources