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!

