Results 1 to 4 of 4
  1. #1
    frente158 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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;
    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

    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
    When you're reading the file back in, read it line-by-line and split each string by the delimiter character.
    Java Code:
      ...
      String[] record = line.split(delimiter);
      ...
    record[0] would then contain the ID
    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.

  3. #3
    frente158 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Read text file with scanner class.

    post your code here and also post any compilation errors so we can find out where its going wrong

Similar Threads

  1. Class employee,company,test: Read data from text file.
    By jeskoston in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 01:50 AM
  2. Using Read/Write From File Using Scanner
    By javaisntcoffee123 in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 03:35 AM
  3. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2009, 02:47 AM
  4. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:50 AM
  5. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •