By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

reading from a text file into a double array

P: 3

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

item, ID, stock, price respectively

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(String [] [] 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
Apr 22 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 10K+
P: 11,448
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:
Expand|Select|Wrap|Line Numbers
  1. class Record {
  2.    private String item;
  3.    private int ID;
  4.    private int stock;
  5.    private double price;
  6.    // ...
  7. }
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.parseInt() and Double.parseDouble()
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,

Apr 22 '07 #2

P: 3
hey jos

thank you so much i will try that tomorrow. i have other more pressing matters today.
Apr 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.