Reputation: 25
I'm trying to create an ArrayList
from data like this taken from a text file called price.txt
. So the A
and the B
are the region codes, the integers are the weight in kilos and the double is the price.
A
15 3.50
25 4.50
35 6.70
50 7.20
B
15 4.70
25 7.20
35 8.60
50 10.50
I've created an object to hold the data.
public class CostList {
private String code; //Holds Region code
private double weight; //Holds weight
private double price; //Holds price
}
But I'm having trouble getting my head around reading it from the text file and then inputting from the text file.
Upvotes: 0
Views: 66
Reputation: 939
I think it would be better you have an object structure like this (names chosen by myself):
public class Item {
private double weight; //Holds weight
private double price; //Holds price
}
and
public class Region{
private String name; //Holds region name
private List<Item> items; //Holds different items for the region (lines in your file)
}
With your solution you have to create for every line an object with the same region, now you have one object for each region, and inside a list with items for this region!
Concerning the file read I would recommend to look at some tutorials like this. You can easily search in google for reading files in java and you will find a lot of different pages with help! If you have trouble with the process you can ask here, but first try for yourself to implement the file - read - operation!
Upvotes: 1