• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Handling  RSS feed

 
Rahul Bajaj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody plz help...
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

You need to "load" the file in memory:
java.io deals with in/out.

You also need to know how to read text:
java.io.BufferedReader might be usefull

I hope the above gives you a few tips. If your still having problems
feel free to reply back.

/Svend Rost
 
Rahul Bajaj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My i/p file is

"Customer Name","Trade Date","Symbol","Quantity","Price","Commissions"
"Rick","1/20/2005","JPP","5","85.56","2.5"
"Rick","2/20/2005","JPP","-9","100.58","2.5"
"Rick","8/1/2005","POL","7","2.3","1.3"
"Rick","8/1/2005","POL","2","2.2","1.3"

and it shoud produce the o/p

"Customer","Symbol","Quantity","Date Bought","Date Sold","Purchase Price","Selling Price","Gain/Loss","Status"
"Rick","JPP","4","","2/20/2005","","399.82","Unknown","B"
"Rick","JPP","5","1/20/2005","2/20/2005","430.3","500.4","70.1","M"
"Rick","POL","9","8/1/2005","","23.1","","Unknown","H"

Which is like selling buying of trades.



The rules of transformations are:



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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks little difficult to me, any hint will be good.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

/Svend Rost
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!