Parsing a file in java

I'm supossed to parse this file, but i'm not told what the contents of the file mean. Here is an example of what the contents look like:

bg&12309876A&TEXAS-CO1234&0.0&2578.0&-14357490.00&0.0&0.0&12000.0

bg&12309876A&New_DAS&-17685953.00&189036983.0&0.0

.

.

.

%%%%%%%%%%%%%%%%%%%%%%%%%%%

bg&123098767&airo-CO15554&0.0&2578.0&-1999490.00&0.3&0.0&12030.0

bg&123098767&Guinea_DAS&-10000053.00&189036983.0&0.0&-1.003

bg&123098767&Gafgea_DAS&-1011053.00&-18904443.0&0.0&-1.003&0.0&0.0

.

.

.

...and so on and so on

I'm assuming the 'bg' signifies beggining of a line, the '&' separates each section and the % signifies the end of the first chunk of information.

I'm not really told what this file is or what i'm supposed to output if anything.

So i'm not sure how i should even begin. I wrote some code, but I'm stuck, i'm not even sure if i'm heading in the right direction.

////*****

import java.io.*;

import java.util.Vector;

public class Tparser {

/*** Method main ***/

public static void main(String[] args) {

// stream to write file

FileInputStream fin;

String FileName = null;

// create an instance of class Vector

Vector myVector = new Vector ();

System.out.println("Enter the name of the file (e.i. Information.bln): ");

// open up standard input

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

BufferedReader br1 = null;

try {

// read in the file name

FileName = br.readLine();

File file = new File(FileName);

br1 = new BufferedReader(new FileReader(file));

String line = "";

while( (line = br1.readLine()) != null )

{

String [] splitArray = line.split("&");

//for (int i = 0; i < splitArray.length; i++) {

myVector.add(splitArray);

//}

//System.out.println('\n');

//System.out.println(line);

} // end while

for (int i = 0; i < myVector.size()-1; i++) {

String [] temp = (String []) myVector.get(i);

for (int j = 0; j < temp.length; j++) {

System.out.println(temp[j]);

}

System.out.println();

}

} // end try

catch (IOException ioe)

{

System.out.println("IO error trying to read the file name!");

System.exit(1);

} // end catch

} // end main

} // end class

Could some one please give me an idea of how i'm supposed to go about parsing this file. Thanks.

[2778 byte] By [JavaKid123a] at [2007-10-2 22:14:26]
# 1

> I'm supossed to parse this file, but i'm not told

> what the contents of the file mean. Here is an

> example of what the contents look like:

>

> bg&12309876A&TEXAS-CO1234&0.0&2578.0&-14357490.00&0.0&

> 0.0&12000.0

> bg&12309876A&New_DAS&-17685953.00&189036983.0&0.0

> .

> .

> .

> %%%%%%%%%%%%%%%%%%%%%%%%%%%

> bg&123098767&airo-CO15554&0.0&2578.0&-1999490.00&0.3&0

> .0&12030.0

> bg&123098767&Guinea_DAS&-10000053.00&189036983.0&0.0&-

> 1.003

> bg&123098767&Gafgea_DAS&-1011053.00&-18904443.0&0.0&-1

> .003&0.0&0.0

> .

> .

> .

> ...and so on and so on

>

> I'm assuming the 'bg' signifies beggining of a

> line, the '&' separates each section and the %

> signifies the end of the first chunk of information.

How are the chunks related? Maybe the % line is just a comment. Who knows?

You don't say how the delimited values are related or what their meaning is. Is the format of each line the same? Same number of values on each one, or can it vary from line to line?

I'd start with an object to act as the container for each record.

I'd write a static method valueOf(String) that would take in a String of that format and return back an instance of this object. I'd also write the toString() method so that it pumped out one of those records for a given object. You have write capability now.

Something like this:

package model;

public object Record

{

public static Record valueOf(String record)

{

// put the code here that splits the record at the '&' and create a Record object from the tokens

String [] tokens = record.split("&");

return new Record(tokens);

}

public String toString()

{

StringBuffer buffer = new StringBuffer(1024);

// load the values from this Record into a StringBuffer with '&' between each one

return buffer.toString();

}

}

Have your reader bring in one line at a time. Split it at the ampersand, load the values into an object, and add the object to a List. Read until the file is exhausted and return the List of objects. Now you can manipulate them to your heart's content.

%

>

> I'm not really told what this file is or what i'm

> supposed to output if anything.

So how are we to know?

> So i'm not sure how i should even begin. I wrote some

> code, but I'm stuck, i'm not even sure if i'm heading

> in the right direction.

Code looks irrelevant here until you figure out how to proceed in English. Once you have that, writing code will be easier.

%

duffymoa at 2007-7-14 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 2

thanks a bunch, i wasn't sure were to go with this! This helps a lot.

> You don't say how the delimited values are related or what their meaning is. Is > the format of each line the same? Same number of values on each one, or

> can it vary from line to line?

Thats the hard part, i'm not told how the values are related or what they mean. It's possibly from a simulation that is being run , and the data from the simulation is compiled into this format. To answer your question, each line is formatted the same, but there are different number of values on each line. And a chuck of about 200 lines is separated by the %%%%%%. I think the percent signs separate each instance of the simulation that is being run. This file is about 10,000 lines.

Thanks, figuring what you want to do and how to do it, is the hard part. Then the coding is the easy part..

JavaKid123a at 2007-7-14 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 3

You keep referring to a "chuck" as if it were a collection of these records that will have an aggregate meaning. You'll have as many instances of Chuck as there are lines with "%%%%%%%".

If that's true, have a Chuck class that contains a List of Records. Let your Chuck class do something like this:

public class Chuck

{

private List records = new ArrayList();

public void addRecord(Record r) { records.add(r); }

public void removeRecord(Record r) { records.remove(r);

public Record getRecord(index i) { return (Record)records.get(i); }

public void setRecord(index i, Record r) { records.set(i, r); }

}

Keep the code that parses this file separate from the Chuck and Record classes. That way you can change parsers without having to change those two classes. Once you get them right, they're always right.

The parser will take an InputStream and return a List of Chucks. Each Chuck will have its List of Records. Now anyone who wants to manipulate this data will have it in what amounts to a tree that would look like this if it were XML:

<test>

<chuck id="1">

<record id="1">

<field><name>x</name><value>10</value></field>

</record>

<!-- lots of records -->

</chuck>

<!-- lots more chucks -->

</test>

%

duffymoa at 2007-7-14 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 4
Maybe you need a Field class with name, value pairs, too.This is just like parsing an EDI file. It's not uncommon. If you get this Chuck/Record/Field/Parser package right you can reuse it all over the place, anytime you have to parse a flat file.%
duffymoa at 2007-7-14 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 5
Sorry about that, it's 'chunk' not 'chuck'Thanks for all your help, I'm just a first year CompSci student and i haven't programmed in java before. I wasn't sure where to get started, i think i know how to do this now. Thanks
JavaKid124a at 2007-7-14 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 6

> Sorry about that, it's 'chunk' not 'chuck'

Silly me. 8)

> Thanks for all your help, I'm just a first year

> CompSci student and i haven't programmed in java

> before. I wasn't sure where to get started, i think i

> know how to do this now. Thanks

Come back with questions if your code needs help.

%

duffymoa at 2007-7-14 1:31:22 > top of Java-index,Java Essentials,Java Programming...