Scanner and reading data from file and parsing into objects HELP!

I have generated a database of names in this format;

1 : Sam: Age 75 : Income 820000

2 : Wolfe: Age 37 : Income 530000

3 : Will : Age 25 : Income 270000

I have implemented the following code;

import java.io.*;

import java.util.Scanner;

publicclass ScanXan{

publicstaticvoid main(String[] args)throws IOException{

Scanner s =null;

try{

s =new Scanner(new BufferedReader(new FileReader("data.dat")));

/*

* A different delimiter spearates the output with a different method of tokenising

* by default, the delimiter is whitespace

*/

while (s.hasNext()){

System.out.println(s.next());

}

}finally{

if (s !=null){

s.close();

}

}

}

/*

* Purpose: The Person class creates instances of the class Person with the instance variables that store information

* about the persons name,age and their income, 3 instance methods are implemented here to return the basic data about a

* particular data which the user may need.

*

*/

publicstaticclass Person

{

// data fields defining the name,age and income

private String name;

privateint age;

privateint income;

// Constructor for Class Person

public Person(String name,int age,int income)

{

this.name = name;

this.age = age;

this.income = income;

}

// Instance method for returning some basic values about personal data

public String toString()

{

System.out.println();

returnnew String("Name : " + getName() +" " +"Age: " + getAge() +" " +"Income : $" + getIncome() +"\n" );

}

// This method returns the name of the instance object

public String getName()

{

return name;

}

// This method returns the age of the instance object

publicint getAge()

{

return age;

}

// This method returns the income of the instance object

publicint getIncome()

{

return income;

}

}

}

What I have to do is, create and array of of instance objects of type Person and fill them up with the name, age and income portions from the data file.

Can anyone help me please?

The current output is;

1

:

Sam

:

Age

75

:

Income

820000

2

:

Wolfe

:

Age

37

:

Income

530000

3

:

Will

:

Age

25

:

Income

270000

Cheers!

[4974 byte] By [SpartanNZa] at [2007-11-27 1:42:04]
# 1

What you have done looks OK so far -- you just need to start creating Person objects using the Strings you are reading in instead of printing them out.

First figure out when you have found a Person's name.

Next change the following two Strings representing their age and income into ints. This can be done with Integer.parseInt()

Then pass that information along to the Person constructor.

Here's an idea.. maybe it can help you!

// create whatever kind of List you want

List<Person> people;

// store information needed to create Person objects

// in a String and two ints

String name = null;

int age = -1;

int income = -1;

while (s.hasNext()) {

String line = s.next();

// figure out if the line is worth processing

// then figure out if it is a name, age, or income

// if name is null, set name to line

// else if age is -1, set age to Integer.parseInt(line)

// else if income is -1, set income to Integer.parseInt(line)

// and then create a Person(name, age, income) and add it to the List

// once the Person has been created, reset name,

// age, and income to their default values

}

ErikSilkensena at 2007-7-12 0:58:13 > top of Java-index,Java Essentials,Java Programming...
# 2

>1 : Sam : Age 75 : Income 820000

Keeping in mind read the whole line

then start parsing

find : then name starts find : name ends.

find Age and then income.

You can use the String.IndexOf(":") to find the position of specific character and the substring.

After extracting all the required elements create the Person object with these values.

jawadhashmia at 2007-7-12 0:58:13 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi! Thank you for your inputs!But i'm still having some trouble wrapping my head around the concept of tokens, how can I filter out the data so that I can only parse the integers into ages and incomes, I know how to store the strings now.Cheers!
SpartanNZa at 2007-7-12 0:58:13 > top of Java-index,Java Essentials,Java Programming...
# 4
you can use StringTokenizer for separating the fields in different tokens. Then remove the extra characters from there.int n = Integer.ParseInt(String);double d = Double.parseDouble(String)
jawadhashmia at 2007-7-12 0:58:13 > top of Java-index,Java Essentials,Java Programming...
# 5
YES! I did it! Thank you all for your wonderful inspiration and help!Cheers!
SpartanNZa at 2007-7-12 0:58:13 > top of Java-index,Java Essentials,Java Programming...