backa gain for more advice

I need to make this:

//Open the input stream from the file

FileInputStream instr =new FileInputStream(file);

//Enable the buffered reader

BufferedReader in =new BufferedReader(new InputStreamReader(instr));

//Holds a line of the file

String fileLine =new String();

//Continue parsing until the end of the file is reached

while((fileLine = in.readLine()) !=null)

{

//This condition applies to the first section of the file

//which does not contain any mole fraction data. The parser

//pulls the x data from this section.

if(fileLine.toLowerCase().indexOf("x(cm)") > -1

&& fileLine.toLowerCase().indexOf("mole fractions") == -1)

{

//Take in a line of data

fileLine = in.readLine();

//The line immediately following all the data has no content

while(fileLine.length() > 0)

{

//X data always starts at position 7 in the file

xValue =new Double(fileLine.substring(7, fileLine.indexOf(" ",7)));

//Convert to mm

xValue =new Double(xValue.doubleValue()*10);

//Set the value in the x column

x.setValue(xValue.toString());

//Take in the next line of data

fileLine = in.readLine();

}

}

//This condition applies to the following sections of the file

//that contain mole fraction data. The parser pulls all y data

//from these sections

if(fileLine.toLowerCase().indexOf("x(cm)") > -1

&& fileLine.toLowerCase().indexOf("mole fractions") > -1)

{

//Count of the columns of molecule data in this section

int columns = 0;

//Take in the line that contains molecule id header info

fileLine = in.readLine();

//Go through the header, creating a new molecule for each

//new molecule id in the header

for(int i = 0; i < fileLine.length(); i++)

{

//Molecule id's are separated by several spaces

if(fileLine.charAt(i) !=' ')

{

//Obtain the id as a substring

formula = fileLine.substring(i, fileLine.indexOf(" ", i));

System.out.print("Enter a name for molecule " + formula

+" :> ");

name = user.readLine();

//Create a new molecule and increase the counters

Column yNew =new Column();

yNew.copyHeader(y);

molecules[counter] =new Molecule(name, formula, x, yNew);

counter++;

columns++;

//Skip the for-loop counter to the next occurence of

//a space

i = fileLine.indexOf(" ", i);

}

}

//Take in the next line of data

fileLine = in.readLine();

//The first molecule index number of a section. We need to

//subtract and extra 1 since Column 1 corresponds with

//Molecule 0 due to the existence of an X column at the

//beginning of each section

int start = counter - columns - 1;

//Temporary holder for data entries

String value =new String();

//Go through a section of y data, parsing data from the columns

//and putting it into the appropriate molecule's y data

while(fileLine.length() > 0)

{

//Counter for the columns in this section

int colSection = 0;

//i represents the character position in the row.

//Starts at 5, which is the start position for the first

//column of data.

for(int i = 5; i<fileLine.length(); i++)

{

if(fileLine.charAt(i) !=' ')

{

if(colSection == 0)

{

i = fileLine.indexOf(" ", i);

colSection++;

}

elseif(colSection > 0 && colSection < columns)

{

value = fileLine.substring(

i, fileLine.indexOf(" ", i));

molecules[start + colSection].y.setValue(value);

i = fileLine.indexOf(" ", i);

colSection++;

}

else

{

value = fileLine.substring(i);

molecules[start + colSection].y.setValue(value);

i = fileLine.length();

colSection++;

}

}

}

fileLine = in.readLine();

}

take in things that are tab delimited like this one can:

while((fileLine = in.readLine()) !=null)

{

int tabIndex = fileLine.indexOf('\t');

x.setValue(fileLine.substring(0,tabIndex));

y.setValue(fileLine.substring(tabIndex+1));

}

Any advice?

[7548 byte] By [aksakataa] at [2007-10-2 14:48:59]
# 1
> I need to make this:Then contact a programming consultant.
aksakataa at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 2
What's wrong with it?
prometheuzza at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 3
The files that the first code takes in are supposed to be in a certain format where they are oddly delimited and it goes by 7th character, but in most of the files that need parsing, this is not the format used.
aksakataa at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 4
> The files that the first code takes in are supposed> to be in a certain format where they are oddly> delimited and it goes by 7th character, but in most> of the files that need parsing, this is not the> format used.Aaaah, I see.
prometheuzza at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 5
any suggestions?
aksakataa at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 6
> any suggestions?Yeah, fix it in a way that the code can also handle oddly formatted text...
prometheuzza at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 7

//Open the input stream from the file

FileInputStream instr = new FileInputStream(file);

//Enable the buffered reader

BufferedReader in = new BufferedReader(new InputStreamReader(instr));

//Holds a line of the file

String fileLine = new String();

//Continue parsing until the end of the file is reached

while((fileLine = in.readLine()) != null)

{

//This condition applies to the first section of the file

//which does not contain any mole fraction data. The parser

//pulls the x data from this section.

if(fileLine.toLowerCase().indexOf("x(cm)") > -1

&& fileLine.toLowerCase().indexOf("mole fractions") == -1)

{

//Take in a line of data

fileLine = in.readLine();

//The line immediately following all the data has no content

while(fileLine.length() > 0)

{

//X data always starts at position 7 in the file

xValue = new Double(fileLine.substring(0, fileLine.indexOf("\t")));

//Convert to mm

xValue = new Double(xValue.doubleValue()*10);

//Set the value in the x column

x.setValue(xValue.toString());

//Take in the next line of data

fileLine = in.readLine();

}

}

//This condition applies to the following sections of the file

//that contain mole fraction data. The parser pulls all y data

//from these sections

if(fileLine.toLowerCase().indexOf("x(cm)") > -1

&& fileLine.toLowerCase().indexOf("mole fractions") > -1)

{

//Count of the columns of molecule data in this section

int columns = 0;

//Take in the line that contains molecule id header info

fileLine = in.readLine();

//Go through the header, creating a new molecule for each

//new molecule id in the header

for(int i = 0; i < fileLine.length(); i++)

{

//Molecule id's are separated by several spaces

if(fileLine.charAt(i) != ' ')

{

//Obtain the id as a substring

formula = fileLine.substring(i, fileLine.indexOf("\t", i));

System.out.print("Enter a name for molecule " + formula

+ " :> ");

name = user.readLine();

//Create a new molecule and increase the counters

Column yNew = new Column();

yNew.copyHeader(y);

molecules[counter] = new Molecule(name, formula, x, yNew);

counter++;

columns++;

//Skip the for-loop counter to the next occurence of

//a space

i = fileLine.indexOf("\t", i);

}

}

I changed the java to look like this and it seemed to be working fine, it took in all of the molecule headers and asked for names until the last one then it said

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -84

at java.lang.String.substring(Unknown Source)

at als.model2.Acquisition.ParseModelFile(Acquisition.java:541)

at als.model2.Acquisition.ModelAcquire(Acquisition.java:270)

at als.model2.Model.main(Model.java:45)

aksakataa at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 8
> fileLine.indexOf("\t", i)What if that returns a value less than i? That'll mess you up. You need to check that value first - don't just blindly use the value.
warnerjaa at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...
# 9

Okay, so how do i go about having the program check that number before it uses it, and how do i get it to actually get the last header? it is stopping reading that line before it goes on further, i had it functioning earlier without asking baout the last header, but i twas at least going smoothly to the next task, now that has stopped as well....

aksakataa at 2007-7-13 13:25:06 > top of Java-index,Java Essentials,Java Programming...