reading from a text file into a double array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathGirl
    New Member
    • Apr 2007
    • 3

    #1

    reading from a text file into a double array

    Hi,

    I am not computer savvy at all. i am a math major so I am utterly lost

    I need to read information form a text file i created called Item

    this is the file


    where
    item, ID, stock, price respectively

    pretzel,0,20,25
    chips,1,20,50
    mints,2,20,75
    gum,3,20,100
    chocolate,4,20, 125
    cookies,5,20,15 0
    trail mix,6,20,175
    pop tarts,7,20,200


    I am creating a class that access this file and then displays and allows me to manipulate the data

    public class VendorClass
    {
    private String [] [] itemList;

    public VendingItems(St ring [] [] itemList);

    this is all i have so far. Our teachr assigned us this project but has not gotten this far in the course. the book we have also does not have examples of how to read double array from a text file, otherwise I would have used that

    Also, I need to know how to selectively change the stock and price into integers not Strings

    any help would be appreciatted. i am going out of my mind
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You don't want to read all that in a two dimensional array, take it from me. You
    want to read every line/record and build a little object from it. Given your elements
    the class could look like this:
    Code:
    class Record {
       private String item;
       private int ID;
       private int stock;
       private double price;
       // ...
    }
    Given a BufferedReader you can read entire lines from your file. The String
    class has a convenient method split() that can split the line around those
    commas. The methods Integer.parseIn t() and Double.parseDou ble()
    give you piecemeal values for your simple Record class objects. Finally you
    can stick all your Records in a List<Record> object, or another collection
    if you prefer. Read the API docs for the appropriate classes and methods.

    kind regards,

    Jos

    Comment

    • mathGirl
      New Member
      • Apr 2007
      • 3

      #3
      hey jos


      thank you so much i will try that tomorrow. i have other more pressing matters today.

      Comment

      Working...