Thanks in advance for your help, everyone.

...So basically, for a program I have to do for this internship, I have to find a way to extract certain lines of temperature, density, and frequency data from a .txt file, and then pop them in 3 different string arrays. A typical file I am supposed to open with my program looks like this:
______________________________________________________________________________

RUN ON MACHINE 4 = CL1HEAD ON 2010/07/08 16:04:36

FOR PHOTOELECTRIC DATA, USED ASCII FILE /opacity/phdat.asc
MATERIAL- TIO RHO =1.000E+00 AVWT = 31.9 AV.Z = 15.0

LILAC MATERIAL NUMBER - 106 WRITTEN IN FORMAT 2

COMPOSITION:
(22) TITANIUM 50.0% A = 47.9 Z = 22.0
( 8) OXYGEN 50.0% A = 16.0 Z = 8.0


DENSITY (G/CM3) 18 POINTS
1.0000E-05 3.1600E-05 1.0000E-04 3.1600E-04 1.0000E-03 3.1600E-03 1.0000E-02 3.1600E-02 1.0000E-01 3.1600E-01
1.0000E+00 3.1600E+00 1.0000E+01 3.1600E+01 1.0000E+02 3.1600E+02 1.0000E+03 3.1600E+03


TEMPERATURE (EV) 17 POINTS
1.0000E+00 3.0000E+00 1.0000E+01 3.0000E+01 1.0000E+02 2.0000E+02 4.0000E+02 6.0000E+02 8.0000E+02 1.0000E+03
1.2500E+03 1.5000E+03 2.0000E+03 2.5000E+03 5.0000E+03 1.0000E+04 2.5000E+04


FREQUENCY (EV) 48 GROUPS
1 5.0000E+01 1.0000E+02 1.5000E+02 2.0000E+02 2.5000E+02 3.0000E+02 3.5000E+02 4.0000E+02 4.5000E+02 5.0000E+02
11 5.5000E+02 6.0000E+02 6.5000E+02 7.0000E+02 7.5000E+02 8.0000E+02 9.0000E+02 1.0000E+03 1.2000E+03 1.4000E+03
21 1.6500E+03 2.0000E+03 2.3000E+03 2.7000E+03 3.1000E+03 3.5000E+03 4.0000E+03 4.5000E+03 5.0000E+03 5.5000E+03
31 6.0000E+03 6.6000E+03 7.2000E+03 8.0000E+03 8.8000E+03 9.3000E+03 1.0000E+04 1.2500E+04 1.5625E+04 1.9531E+04
41 2.4414E+04 3.0518E+04 3.8147E+04 4.7684E+04 5.9605E+04 7.4506E+04 9.3132E+04 1.0000E+05

_____________________________________________________________________________________

See all those numbers in bold? Those are the numbers I have to put in string arrays. The thing is though...I don't know how to really extract just those values from the .txt file! The tricky thing about it is I only want the values, I don't want or need the italicized words.

Any help would deeply be appreciated. Thank you,

Recommended Answers

All 5 Replies

Write the program in steps. First step is to read in the lines from the file.
To verify its working, use System.out.println(...) to show them.

When you can do that, we'll move on to the next step: parsing out the values you want.

Write the program in steps. First step is to read in the lines from the file.
To verify its working, use System.out.println(...) to show them.

When you can do that, we'll move on to the next step: parsing out the values you want.

Alright, Ill work on getting the whole chunk onto a println. Ill post the code when I do.

Okay, so heres the code for the entire .txt file. I took out the begginning parts with the imports etc and just got down to the main part. Java outputs the entire thing:

System.out.println("Enter File name [ie - Enternamehere.text]");
    	filename = input.next();
    	File textfile = new File("c:\\"+filename);
    	// Tests to see if file actually exists at [C:\\]
    	// If so, proceeds to output whole file.
    	
    		FileReader in;
    		BufferedReader readfile;
    		String lineoftext;
    		try {
    			in = new FileReader(textfile);
    			readfile = new BufferedReader(in);
    			
    			while ((lineoftext = readfile.readLine()) != null ) {
    			System.out.println(lineoftext);
    			}
    			readfile.close();
    			in.close();
    	} catch (FileNotFoundException e) {
    	  System.out.print("File does not exist at C:\\. Please recheck spelling and follow tutorial");
    	} catch (IOException e) {
    		System.out.print("Problem Reading File");
    	}

...So now that I have this, how do I parse the values I want?
Thank you for your help NormR1.

Second phase will be to recognize the boundaries between the 3 types of data.
Looking at the data for the file you posted earlier, it appears there are several sections. The first to be skipped up to the beginning of the "Density" line, then all the density lines need to be read and saved up to the end of them which is indicated by the "tempurature" line after which you read and save that data up to the next data type where you'll read and save the lines for it.

Do you know how many lines there are to be saved for each type? Or a max count?
If you use a String array you'll need to define it large enough to hold your maximum number of lines.
A better way to save the Strings would be in a ArrayList or Vector.

The phase after that will be to break each line of data up into individual parts.

Second phase will be to recognize the boundaries between the 3 types of data.
Looking at the data for the file you posted earlier, it appears there are several sections. The first to be skipped up to the beginning of the "Density" line, then all the density lines need to be read and saved up to the end of them which is indicated by the "tempurature" line after which you read and save that data up to the next data type where you'll read and save the lines for it.

Do you know how many lines there are to be saved for each type? Or a max count?
If you use a String array you'll need to define it large enough to hold your maximum number of lines.
A better way to save the Strings would be in a ArrayList or Vector.

The phase after that will be to break each line of data up into individual parts.

Your correct, the data will vary depending on the file. Therefore, I should use ArrayList's like you said. The problem is, I don't know how to store strings from .txt files in an arraylist. I am looking into it right now. If I have success, I'll post the code here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.