Hi all
import java.io.*;
public class POManager {
private static String posFname = "C:/ITM200/OOP-Advanced-Topics/src/po.txt";
private static String taxposFname = "C:/ITM200/OOP-Advanced-Topics/src/tax.txt";
public static void main(String [] args) throws IOException {
PO[] pos = readPOFile();
writePOTaxFile(pos);
}
public static PO[] readPOFile() throws IOException {
BufferedReader in = new BufferedReader(new FileReader(posFname));
in.readLine();
String line = in.readLine();
int n = Integer.parseInt(line.trim());
PO[] pos = new PO[n];
int cntr=0;
while( (line = in.readLine()) != null && cntr<n){
pos[cntr] = readPOInfo(line.trim());
cntr ++;
}
in.close();
return pos;
}
public static PO readPOInfo(String line){
//name,date,pid,pname,uprice,quanity
String[] fields = line.split(",");
Product p = new Product(fields[3],Double.parseDouble(fields[4]),
Integer.parseInt(fields[5]));
PO po = new PO(fields[0],fields[1],Integer.parseInt(fields[2]),p);
return po;
}
public static void writePOTaxFile(PO[] pos) throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter(taxposFname));
for (int i = 0;i<pos.length;i++){
out.write(pos[i].toString() +"\n");
System.out.println("write ..." + pos[i].toString());
}
out.close();
}
}
and 2 classes:
public class PO {
protected String name;
protected String date;
protected int pid;
protected Product product;
public double tax;
public PO(String name_, String date_, int pid_, Product p)
{
name = name_;
date = date_;
pid = pid_;
product = p;
}
public String toString()
{
String s = name + "," + date + "," + pid + "," + product;
return s;
}
}
public class Product{
protected int quanity;
protected String type;
protected double uprice;
public double tax;
public Product(String type_, double uprice_, int quanity_)
{
type = type_;
uprice = uprice_;
quanity = quanity_;
}
public void tax()
{
tax = 0.15*uprice*quanity;
}
public String toString()
{
String s = type + "," + uprice +"," + tax;
return s;
}
}
the txt input is sth like this:
2
A,Jan-02-2007,11,table,10.56,1
B,Feb-12-2007,14,chair,6.3,4
when i tried to run the program, here what i got:
Exception in thread "main" java.lang.NumberFormatException: For input string: "A,Jan-02-2007,11,table,10.56,1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at POManager.readPOFile(POManager.java:14)
at POManager.main(POManager.java:7)Process completed.
Dont know where i got it wrong :(