Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: Reading from file.... Posted by zibadian on 30 Apr 2007 at 9:04 AM
: : 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.
Thread Tree
Archestic Reading from file.... on 30 Apr 2007 at 1:52 AM
zibadian Re: Reading from file.... on 30 Apr 2007 at 2:11 AM
Archestic Re: Reading from file.... on 30 Apr 2007 at 8:48 AM
zibadian Re: Reading from file.... on 30 Apr 2007 at 9:04 AM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.