File Handling

Rahul Bajaj

Ranch Hand

Posts: 45

posted 11 years ago

Hello, I am working on a program where I have a input file seperated by comma. I want to pull out the different columns and based on there values need to do some calculations and again generate output file with the result. I have to give Input file location, Customer name and output file location from command line and rows with the customer name given will be selected and operations will be performed only on those rows.

How can I achieve that? means how to extract columns info?

Rahul Bajaj

Ranch Hand

Posts: 45

posted 11 years ago

Somebody plz help...

Svend Rost

Ranch Hand

Posts: 904

posted 11 years ago

Hi Miki,

Sounds a bit like a school project.. so I'll try to give advice rather than a solution.

As I understand your problem, you got a (text) file which contains some data (a comma separated list) that needs to be computed. For example:

and for each column (i.e. (1,1,1), (4,3,3) and so forth) you need to perform an operation. An idea might be to 1) load each row in some kind of a datastructure (array, linkedlist ect.) and 2) perform the operation on each column.

Now, how do you extract column information? well, there are many ways.. how do you think you can extract it? What ideas do you have?

1. Customers can only sell stocks that they already own. (So if a person did not buy a stock in 2005 but then sold it in 2005 we assume that he bought it before 1/1/2005.)

2. Transactions done on the same date for the same stock should be combined together for the output, with commissions and sales or purchase prices added.

3. There are three resulting states:

M=stock bought in the current year and sold in the current year,

B=stock bought before the current year and sold in the current year,

H=stock bought in the current year and held thru the end of the year.

4. Sales price = quantity x price � commission

5. Purchase price = quantity x price + commission

6. Gain/Loss = Sales Price � Purchase Price

Rahul Bajaj

Ranch Hand

Posts: 45

posted 11 years ago

Looks little difficult to me, any hint will be good.

Svend Rost

Ranch Hand

Posts: 904

posted 11 years ago

Hi again,

try to solve the assignment by solving it in "small steps"

Try to 1. impl. a loadFile(String input_location) method which loads the file 2. impl. a printLines(File myFile) which prints all the lines in the file (hint: loop) 3. impl. the two method: - loadData(File myFile) which parses the lines in the file, and inserts all the elements in a datastructure (e.g. String array). Here you need to read a line, and then parse that string char by char. - printDataStructure() prints the datastructure that contains the data you just substracted from the file 4. Try to work on the transformations 5. Try to implement a writeResult() method which writes data to the output file.

Take a look at the links I posted earlier... this should get you started.