Results 1 to 4 of 4
- 03-10-2012, 02:44 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Read text file with scanner class.
Hi, the program should read the text file below. This is suppose to be a history of transactions. Where the first number is the customer ID, then the name of the investment with the type, then the number of transactions follow by the date, type (sell or buy) the number of shares and price.
32242342
IBM,stock
6
20050426,B,30,25.00
20050616,B,50,27.00
20060326,S,50,30.00
20070226,S,30,35.00
20080401,B,10,40.00
20080401,S,10,50.00
ABC,fund
6
20050326,B,10,35.00
20050416,B,20,37.00
20090101,B,40,50.00
20060826,S,30,40.00
20060826,S,20,40.00
20090926,S,20,45.00
XYZ,stock
0
NMA,fund
0
MOBC,stock
0
The program have a class call HistoryItem (below). What I have so far is just reading the file, class call ReadFile, but I need to store in variables the information date, shares and price. Any help I would appreciate. Thanks.
//ReadFile class
package portfolio_manager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class ReadFile {
/**Constructor.
@param aFileName full name of an existing, readable file.*/
public ReadFile(String aFileName){
fFile = new File(aFileName);
}
public final void processLine()throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader(fFile));
scanner.useDelimiter(",");
while(scanner.hasNextLine())
{
String date = scanner.nextLine();
System.out.println(date);
}
scanner.close();
}
private final File fFile;
}
//HistoryItem class
package portfolio_manager;
import java.util.UUID;
class HistoryItem {
UUID id;
String date;
int shares;
double price;
HistoryItem(String date, int shares, double price)
{
id = UUID.randomUUID();
this.date = date;
this.shares = shares;
this.price = price;
}
}
-
Re: Read text file with scanner class.
If you write the file in a CSV format, that should make it easier for you to read your variables back in.
CSV is a comma-delimited table, for example you have a table with four columns: id, date, shares, price
So all you have to do is write these four variables each separated by a comma(","), then create a new line for each record.
Your logic to write the file should be like this:
When you're reading the file back in, read it line-by-line and split each string by the delimiter character.Java Code:final Char delimiter = ',' final Char newline = '\n' Open text file For each Item in itemsArray String line = Item.id + delimiter + Item.date + delimiter + Item.shares + delimiter + Item.price + newline //write record to text file write(line) End For Close text file
record[0] would then contain the IDJava Code:... String[] record = line.split(delimiter); ...
record[1] would contain the Date
record[2] would contain the Shares
record[3] would contain the Price.
You'll need a method to create an instance of your HistoryItem object after reading those values from the text/csv file.
- 03-11-2012, 06:30 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: Read text file with scanner class.
Thanks for your reply. I tried but it doesn't seem to work, most likely is my code, I'm not able to translate it to actual code. I'll keep trying.
-
Similar Threads
-
Class employee,company,test: Read data from text file.
By jeskoston in forum New To JavaReplies: 4Last Post: 03-01-2011, 01:50 AM -
Using Read/Write From File Using Scanner
By javaisntcoffee123 in forum New To JavaReplies: 4Last Post: 04-15-2010, 03:35 AM -
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2009, 02:47 AM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:50 AM -
Read from console (Scanner Class)
By hey in forum New To JavaReplies: 10Last Post: 12-11-2007, 10:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks