: : The simplest way is to store each customer in an array (or list) of
: : strings. Then make a method in your data object, which loops through
: : the array, splits it at the =-sign, and sets the fields based on the
: : name. Here's a short example:
: :
: :
: : public void readFileRecord(String[] dataFields) {
: : String fieldName;
: : String fieldValue;
: : for (i = 0; i < dataFields.length; i++) {
: : fieldName = dataFields[i].substring(0, dataFields[i].indexOf("=")-1);
: : fieldValue = dataFields[i].substring(dataFields[i].indexOf("="));
: :
: : if (fieldName.equalsIgnoreCase("name"))
: : this.name = fieldValue;
: : else if (fieldName.equalsIgnoreCase("doors"))
: : this.doors = Integer.parseInt(fieldValue);
: : else ...
: : }
: : }
: : : :
: : You need to change this code to match the rest of your program.
:
: In the case of this example what would be in the 'dataFields'??? I
: was a little confused about how that might be intergrated in with my
: code.
:
: I am also finding that the 'this.name = fieldValue' wont work
: because the 'this' cannot be used in a static context.
:
:
: ----------
: ArcH
dataFields would hold the lines for 1 customer, 1 line per string. This method should be part of a class designed to hold the data 1 customer, as such it cannot be static.
Fuller example:
public class Customer {
private String name;
private int doors;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDoors() {
return doors;
}
public void setDoors(int doors) {
this.doors = doors;
}
public void readFileRecord(String[] dataFields) {
String fieldName;
String fieldValue;
for (int i = 0; i < dataFields.length; i++) {
fieldName = dataFields[i].substring(0, dataFields[i].indexOf("=")-1);
fieldValue = dataFields[i].substring(dataFields[i].indexOf("=")
);
if (fieldName.equalsIgnoreCase("name"))
this.name = fieldValue;
else if (fieldName.equalsIgnoreCase("doors"))
this.doors = Integer.parseInt(fieldValue);
//else ...
}
}
}
To create and read a customer, you simply fill the dataFields with the lines read from the file. Here's what the fields and their values look like after loading:
dataFields[0] = "note = \"not an easy man to see --- Des\"";
dataFields[1] = "name = \"Eldon Tyrell\"";
dataFields[2] = "address = \"c/o Tyrell Corporation, Tyrell Tower, London\"";
etc..
You can then use a code like this to fill the customer:
Customer customer = new Customer();
customer.readFileRecord(dataFields);
Now you can use the various getter/setter methods of the Customer class to read or change the values.