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 :(

You are trying to turn something into an Integer that cannot be turned into an Integer.
You are trying to turn "A,Jan-02-2007,11,table,10.56,1" into an Integer, which you can't do..
That is why you are getting a NumberFormatException. What exactly do you want to turn into an Integer..?

Im not trying to turn "A,Jan-02-2007,11,table,10.56,1" into integer
I turned the 1st line in txt file, which is "2" into integer in line 14. The rest were added to Product and PO class. I used split to split them and change them to int,double,string separately

In the function readPOFile() you are calling in.readLine() twice, so will it not be the second line that you are reading:

public static PO[] readPOFile() throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(posFname));

		// Calling it twice here....
                in.readLine();
		String line = in.readLine();
		int n = Integer.parseInt(line.trim());

So if the second line is "A,Jan-02-2007,11,table,10.56,1" then you are assigning the variable line to be that line, and then preceding to try to turn that into an Integer.

oh thanks
didnt notice that i typed readline 2 times

problem solved now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.