Reading from file....

I am currently on a project where i need to read in a list of customers from a text file.

This file holds the name of the customer, avalible funds, type of car they are after, and other details (the thing to to note is not all of the fields are there for all of the customers, and some feilds are blank).

Example:

[italic]@customer {
note = "not an easy man to see --- Des",
name = "Eldon Tyrell",
address = "c/o Tyrell Corporation, Tyrell Tower, London",
phone = "01715804411",
type = "hatch",
also = "high performance"
}

@customer
{ name = "Roy Batty",
address = "The Colonies, Mount Pleasant Road, Exeter",
phone = "07874249181",
doors = "5",
note = "doesn't have much time --- Dominique"
}

@customer {
name = "Felix Gaff",
address = "4b Victoria Street, Exeter",
price = "15000",
type = "Estate",
bedrooms = "5",
note = "a time-waster --- Dji"
}[/italic]



What i need to do is read in the file (which i can do easily) then turn each of the customers into a object which can be entered into an array.

As i mentioned before i have a read in method setup so its just the converting of this data into an object.


Any help will be much appreciated

--------------
ArcH

Comments

  • : I am currently on a project where i need to read in a list of
    : customers from a text file.
    :
    : This file holds the name of the customer, avalible funds, type of
    : car they are after, and other details (the thing to to note is not
    : all of the fields are there for all of the customers, and some
    : feilds are blank).
    :
    : Example:
    :
    : [italic]@customer {
    : note = "not an easy man to see --- Des",
    : name = "Eldon Tyrell",
    : address = "c/o Tyrell Corporation, Tyrell Tower, London",
    : phone = "01715804411",
    : type = "hatch",
    : also = "high performance"
    : }
    :
    : @customer
    : { name = "Roy Batty",
    : address = "The Colonies, Mount Pleasant Road, Exeter",
    : phone = "07874249181",
    : doors = "5",
    : note = "doesn't have much time --- Dominique"
    : }
    :
    : @customer {
    : name = "Felix Gaff",
    : address = "4b Victoria Street, Exeter",
    : price = "15000",
    : type = "Estate",
    : bedrooms = "5",
    : note = "a time-waster --- Dji"
    : }[/italic]
    :
    :
    :
    : What i need to do is read in the file (which i can do easily) then
    : turn each of the customers into a object which can be entered into
    : an array.
    :
    : As i mentioned before i have a read in method setup so its just the
    : converting of this data into an object.
    :
    :
    : Any help will be much appreciated
    :
    : --------------
    : ArcH
    :
    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:
    [code]
    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 ...
    }
    }
    [/code]
    You need to change this code to match the rest of your program.
  • : 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:
    : [code]:
    : 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 ...
    : }
    : }
    : [/code]:
    : 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
  • : : 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:
    : : [code]: :
    : : 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 ...
    : : }
    : : }
    : : [/code]: :
    : : 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:
    [code]
    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 ...
    }
    }
    }
    [/code]
    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:
    [code]
    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..
    [/code]
    You can then use a code like this to fill the customer:
    [code]
    Customer customer = new Customer();
    customer.readFileRecord(dataFields);
    [/code]
    Now you can use the various getter/setter methods of the Customer class to read or change the values.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories