Read file to Array

Hi there,

I'm having problems reading a text file into an array. The contents of my file are like this -

Name Address Date of Birth Occupation Wage

JimX Street01/01/01Plumber20000

BobY Road06/06/90Doctor 60000

I've over ever dealt with files containing one piece of information per line, so I'm slightly confused as to how I read this data into an array and spit it up so I can make use of the data later on (i.e. list all people in a certain job or calculate average wage). I've used tokenizers to split by certain characters before (space or tab), but this has me stumped.

Can someone point me in the right direction?

Any help would be very much appreciated.

Thanks.

[727 byte] By [vinceJa] at [2007-11-27 2:55:52]
# 1

I suggest that you instead use an list, like arrayList, and fills it with objects that suits your purpose. Like and class Person, which contains getters and setters for the information that you want to store.. Then add that Person object to the list.

First, read 1 row, split it up with stringtokenizer or whatever. Then add each of these to the object Person by using metods like; setName, setStreet and so on.

Secondly, add this object to the arraylist by, list.add(obj);

Perhaps this is to complicated for your use, but if your not doing it like that then your gonna need an 2d array...

Message was edited by:

prigas

prigasa at 2007-7-12 3:33:01 > top of Java-index,Java Essentials,Java Programming...
# 2

Ok, I see where you are coming from. I understand how to use tokenizers to break up the string, or even using the String.split method. However, when I break my string up, how do I say name = (1st part of string), address = (2nd part of string) and so on ? I'm not sure if I'm being clear here....

Thanks.

vinceJa at 2007-7-12 3:33:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Okay, I thought you said you knowd that part.. Anyhow I suggest using String.split().

Example:

String s = "Jim X Street 01/01/01 Plumber 20000";

Like this: String array[] = s.split(" ");

Now it should split at each "space" it gets to, then you get the first word by:

String firstWord = array[0];

This is what you wondered right?

prigasa at 2007-7-12 3:33:02 > top of Java-index,Java Essentials,Java Programming...
# 4

Ah, I think I understand...

So if I broke the string into 5 parts, I'd have:

String name = array[0]

String address = array[1]

etc

etc

etc

Or does this mean there are 5 separate arrays (1 for each column heading), would my array read like this -

[0] - [Name] [Address]..........[Salary]

[1]- [Name2] [Address2] ...[Salary2]

What I'm trying to figure out is how my data is stored, so I can then do a lookup for a certain job or address or salary etc

Or perhaps do a search for a particular name and display all their details. If they were in separate arrays, would this even be possible?

Cheers

vinceJa at 2007-7-12 3:33:02 > top of Java-index,Java Essentials,Java Programming...
# 5

Here, I wrote you an complete example.

The only thing you need to do now is write these two rows into an file named file.txt and save it at C:

Jim XStreet 01/01/01 Plumber 20000

Bob YRoad 06/06/90 Doctor 60000

Also if you wanna use spaces at the X and Y, then I suggest that you use something else as an seperator like: ';' or '&' or simular instead of space.

I hope this helps as well as aint to complicated...

The poeple class:

public class People {

private String name;

private String address;

private String birthDate;

private String occupation;

private String Wage;

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public String getBirthDate() {

return birthDate;

}

public void setBirthDate(String birthDate) {

this.birthDate = birthDate;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getOccupation() {

return occupation;

}

public void setOccupation(String occupation) {

this.occupation = occupation;

}

public String getWage() {

return Wage;

}

public void setWage(String wage) {

Wage = wage;

}

public People() {

}

}

The HandlingPeopleClass:

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class HandlingPeopleClass {

private List<People> listOfPeople = new ArrayList<People>();

public HandlingPeopleClass() {

readPeopleFromFile();

for(int i = 0; i < listOfPeople.size(); i++) {

People people = new People();

people = (People) listOfPeople.get(i);

System.out.println(people.getName() + " " + people.getAddress() + " " + people.getBirthDate()

+ " " +people.getOccupation() + " " + people.getWage());

}

}

public static void main(String[] args) {

new HandlingPeopleClass();

}

private void readPeopleFromFile() {

People pep = new People();

try {

BufferedReader br = new BufferedReader(new FileReader("C:\\file.txt"));

String fileLine = br.readLine();

while(fileLine != null) {

String array[] = fileLine.split(" ");

pep.setName(array[0]);

pep.setAddress(array[1]);

pep.setBirthDate(array[2]);

pep.setOccupation(array[3]);

pep.setWage(array[4]);

listOfPeople.add(pep);

fileLine = br.readLine();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Also all information is as you probably will notic stored in the listOfPeople List. From there you can get all the information you want, its simple to search through it with an forloop comparing names.. Just have a look and I think you get it.

Message was edited by:

prigas

prigasa at 2007-7-12 3:33:02 > top of Java-index,Java Essentials,Java Programming...
# 6
Hey, this looks ideal. Thank you ver y much for your time! I now understand how to split the string and put the values into the array i choose. My only other problem is the player class - it says there is a missing return statement?Message was edited by: vinceJ
vinceJa at 2007-7-12 3:33:02 > top of Java-index,Java Essentials,Java Programming...
# 7
>My only other problem is the player class - it says there is a missing return>statement?.... It does? Which return statement? Its no problem for me when I run it..
prigasa at 2007-7-12 3:33:02 > top of Java-index,Java Essentials,Java Programming...
# 8

I also noted another problem lool :(:( embaressed :S

This is the corrected readPeopleFromFile..

private void readPeopleFromFile() {

People pep;

try {

BufferedReader br = new BufferedReader(new FileReader("C:\\File.txt"));

String fileLine = br.readLine();

while(fileLine != null) {

pep = new People();

String array[] = fileLine.split(" ");

pep.setName(array[0]);

pep.setAddress(array[1]);

pep.setBirthDate(array[2]);

pep.setOccupation(array[3]);

pep.setWage(array[4]);

listOfPeople.add(pep);

fileLine = br.readLine();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

Also the constructor in the peopleclass should be changed if your planning to use it...

To this.

public People() {

name = "";

address = "";

birthDate = "";

occupation = "";

Wage ="";

}

prigasa at 2007-7-12 3:33:02 > top of Java-index,Java Essentials,Java Programming...