The Artima Developer Community
Sponsored Link

Java Answers Forum
reading in a csv file

1 reply on 1 page. Most recent reply: Feb 23, 2003 9:10 AM by Oliver

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
clare summers

Posts: 1
Nickname: clare123
Registered: Feb, 2003

reading in a csv file Posted: Feb 20, 2003 12:54 PM
Reply to this message Reply
Advertisement
Hi
I wondered if someone can help me with this
I have text which is in a csv file and i want to read it in
Incoming file:
OrderID,CustomerID,ProductName,UnitPrice,Quantity,CategoryName
10248,clar e,Queso Cabrales,21.0000,12,Dairy Products
10248,lauren,Singaporean Hokkien Fried Mee,14.0000,10,Grains/Cereals
10248,clare,Mozzarella di Giovanni,34.8000,5,Dairy Products
10249,lauren,Manjimup Dried Apples,53.0000,40,Produce
and i want to put the output into another file called output, and i want the output in that file to look like this:

clare spent 56.800
lauern spent 67.000
----
17 'Dairy Products' were sold
10 'Grains/Cereals' were sold
40 'Produce' were sold
Could someone please help me i'm realy stuck
thanks
clare


Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: reading in a csv file Posted: Feb 23, 2003 9:10 AM
Reply to this message Reply

//
// usage:java CSVTest <csv file name>
// example: java CSVTest list.txt
//

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class CSVTest {
private HashMap customerSpent;
private HashMap productSold;
private BufferedReader in;
private Pattern delimiter;

public CSVTest(String salesList) throws IOException{
customerSpent = new HashMap();
productSold = new HashMap();
delimiter = Pattern.compile(",");

in = new BufferedReader(
new FileReader(salesList));

}

public String getOneRecord() throws IOException{
return in.readLine();
}

public void produceOneRecord(String record){
String customerName;
String productName;
double price;
int quantity;
double spent ;

String [] items = delimiter.split(record);
customerName = items[1].trim();
productName = items[5].trim();
price = Double.parseDouble(items[3]);
quantity = Integer.parseInt(items[4]);
spent = price * quantity;

Object o = customerSpent.get(customerName);

//new customer
if(o == null){
customerSpent.put(customerName, new Double(spent));
}
//customer already exists
else{
double oldSpent = ((Double)o).doubleValue();
customerSpent.put(customerName, new Double(oldSpent + spent));
}

o = productSold.get(productName);
//new product
if(o == null){
productSold.put(productName, new Integer(quantity));
}
//product already exists
else{
int oldQuantity = ((Integer)o).intValue();
productSold.put(productName, new Integer(oldQuantity + quantity));
}
}

public void printResult(){
Set customerSet = customerSpent.keySet();
Set productSet = productSold.keySet();

for(Iterator itr = customerSet.iterator(); itr.hasNext(); ){
String customer= (String)itr.next();
System.out.println( customer + " spent " +
customerSpent.get(customer));
}

System.out.println("\n-------\n");

for(Iterator itr = productSet.iterator(); itr.hasNext(); ){
String product= (String)itr.next();
System.out.println( productSold.get(product) + " " + product
+ " were sold " );
}
}


public static void main(String [] args) throws IOException{
if(args.length == 0){
System.out.println("usage: java CSVTest <csv file name>");
return;
}

String record;

CSVTest ct = new CSVTest(args[0]);
while((record = ct.getOneRecord()) != null){
ct.produceOneRecord(record);
}

ct.printResult();
}

}

Flat View: This topic has 1 reply on 1 page
Topic: JSP Connect mysql problem Previous Topic   Next Topic Topic: Java and XML

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use