Matt
Matt

Reputation: 2673

Java: Reading CSV file to take min, max, average of various stats

I want to write a Java class that will take a CSV file and take the min, max, average, etc... of various statistics. Please see below for a sample of this file. For example, I want to get the stats for "Opening login page", "Logging in as lead user", etc..

I have started by reading TestName and Time into a Map:

static Map<String, String> data = new TreeMap<String, String>();
...
for (String line : file) {
if (line.contains("Test Name")){
//Get next line, this is the first one.
continue;
}
String TestName = (line.substring(0,line.indexOf(","))).trim();
String Time = (line.substring(line.lastIndexOf(",")+2, line.length())).trim();
//System.out.println(TestName + " " + Time);
data.put(TestName, Time);
}

This works fine. However, I'm not sure of the best way to go about getting all unique values and running the calculation. I don't need to use a Map, it was what just popped into my head. Does anyone know how to do something like this?

CSV file sample:

Test Name, Thread No, Run No, Time(s)
Opening login page, 0, 0, 1.8869998455047607
Opening login page, 1, 0, 2.246999979019165
Opening login page, 2, 0, 2.1710000038146973
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as lead user, 0, 0, 23.616000175476074
Opening login page, 13, 0, 2.125999927520752
Opening login page, 15, 0, 1.8939998149871826
Logging in as lead user, 3, 0, 20.244999885559082
Logging in as lead user, 2, 0, 23.039999961853027 

Upvotes: 0

Views: 3340

Answers (3)

Costi Ciudatu
Costi Ciudatu

Reputation: 38225

You can make use of SQL on top of a CSV file by using some CSV JDBC driver like this one or this one. Once you have loaded such a driver, you can perform whatever type of query you need by using ANSI SQL.

Upvotes: 3

Ryan Castillo
Ryan Castillo

Reputation: 1125

Two things:

  1. For counting the frequency of phrases you could use a HashTable that uses the phrase as the key and increments by one each time the phrase is encountered.
  2. You could make your life a little easier by using a CSV parser library.

Upvotes: 1

Raman
Raman

Reputation: 1517

For parsing csv, refer http://www.java-examples.com/parse-csv-file-using-stringtokenizer-example

You can use Set as u need unique values. For stats, use functions of Collections class like Collections.max(hashSetObj).

Upvotes: 1

Related Questions