/** * Converts the table in domfin.txt to separate .dat files - one for each column. * @author Robert J Morton UK-YE572246C * @version 22 August 2000 */ // This program uses the Java 1.1.8 API. import java.io.*; class domfin { public static void main(String args[]) throws IOException { int ROWS = 36, // number of rows in table including heading row rows = ROWS - 1, // number of rows excluding the title row COLS = 46, // number of columns in table cols = COLS - 2, // number of columns excluding Age & Year columns /* For the numeric values in the body of the table. These are multiplied up to integral pence except for the kWh columns which are left as they are. */ T[][] = new int[cols][rows]; /* For all but the first two column headings. The Age and Year columns are not relevant to this program. They are for completeness only. */ String H[] = new String[cols], s; // to hold an imput line of text /* Open the HTML output file, wrap it in an output stream writer and wrap that in a buffered writer so we can use writeLine() */ BufferedWriter o = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("domfin.htm") ) ); /* COPY THE HTML HEAD FILE INTO THE MAIN HTML FILE Open the input table file and wrap it in an input stream reader and wrap that in a buffered reader so we can use readLine() */ BufferedReader head = new BufferedReader( new InputStreamReader( new FileInputStream("head.htm") ) ); /* Read in the next line of the head file copy it into the main HTML file then close the head file. */ while((s = head.readLine()) != null) o.write(s, 0, s.length()); head.close(); /* READ TABLE'S HEADINGS LINE FROM TEXT FILE 'domfin.txt' SAVE IT TO HEADINGS STRING ARRAY ... 'H[]' AND WRITE IT TO THE HTML FILE ... 'domfin.htm' Open the input table file and wrap it in an input stream reader and wrap that in a buffered reader so we can use readLine() */ BufferedReader r = new BufferedReader( new InputStreamReader( new FileInputStream("domfin.txt") ) ); String h = ""; o.write(h, 0, h.length()); // write the HTML table tag o.newLine(); // end of HTML line of table o.write("", 0, 18); // write the HTML row start tag s = r.readLine(); // read the column headings line from the input file int c = '\t', // tab character for testing p = 0, q = 0; // start and end pointers for current column for(int i = 0; i < COLS; i++) { // for each column in the table if((q = s.indexOf(c, p)) == -1) // if no [further] tab character q = s.length(); // found, end tab is string length h = s.substring(p, q); // extract current column heading if(i > 1) // store all but the first two H[i - 2] = h; // column headings in the array h = ""; // heading text for this column o.write(h, 0, h.length()); // write HTML table column heading p = q + 1; // Next time's start position = 1 + this times end position } o.write("", 0, 5); // write end of html heading row o.newLine(); // end of HTML line of table /* READ IN EACH ROW OF THE TABLE FROM THE TEXT FILE 'domfin.txt' WRITE IT TO THE HTML FILE ... 'domfin.htm' CONVERT STRING VALUE IN £ TO INTEGER PENCE SAVE THIS IN THE 2D INTEGER ARRAY ... 'T[][]' */ int row = 0; //start at the first row of the array // read in the next line of the table while((s = r.readLine()) != null) { p = 0; q = 0; // clear start and end tab positions for column o.write("", 0, 18); // write HTML row start tag for(int i = 0; i < COLS; i++) { // for each columns of this line if((q = s.indexOf(c, p)) == -1) // if no [further] tab char found, q = s.length(); // then end tab is string length h = s.substring(p, q); // extract current column string if(i > 1) { // skip age & year cols 0 & 1 int x = 0; // integer value of column in integral pence /* Try to convert the substring containing the numeric string of the current column to a Double-precision floating-point OBJECT and then convert this to a double value, multiply it by 100 to change £s to pence, round it to the nearest integral value of pence and cast it to an integer value. */ try { Double d = Double.valueOf( s.substring(p, q) ); x = (int)Math.round( d.doubleValue() * 100 ); } /* If sub-string in this column wouldn't parse into a numeric value, use a zero value for the column. */ catch(NumberFormatException e) { x = 0; h = "0"; } // Save integer value in array. i - 2 because it's the first T[i - 2][row] = x; } // The two columns (Age & Year) are not included. h = ""; // form the HTML table column o.write(h, 0, h.length()); // write it to the HTML file // Next time's start position = 1 + this times end position. p = q + 1; } // end of 'for each columns of this line' o.write("", 0, 5); // write the HTML row terminator o.newLine(); // end of HTML line of table row++; // move on to next row } o.write("
" + h + "
" + h + "

", 0, 12); // write the HTML table end-tag o.newLine(); // end of HTML table r.close(); // close the text file /* WRITE THE TABLE COLUMN HEADINGS TO SERVE AS THE KEY */ o.write("

Key To Column Headings

"); o.newLine(); o.write(""); o.newLine(); /* Open the input table file and wrap it in an input stream reader and wrap that in a buffered reader so we can use readLine() */ BufferedReader kt = new BufferedReader( new InputStreamReader( new FileInputStream("tabkey.txt") ) ); for(int i = 0; i < cols; i++) { // For each of table's column headings: try { // Try to get s = kt.readLine(); // its explanation line from the file. } catch(IOException e) { // If no explanation is present, s = "[unspecified]"; // give it as unspecified. } // Write the heading and explanation line to the file. o.write(""); o.newLine(); } o.write("
" + H[i] + "" + s + "
"); // write the end-of-table tag o.newLine(); // end of HTML line kt.close(); // close the headings explanations file /* COPY THE HTML TAIL FILE INTO THE MAIN HTML FILE Open the input table file and wrap it in an input stream reader and wrap that in a buffered reader so we can use readLine() */ BufferedReader tail = new BufferedReader( new InputStreamReader( new FileInputStream("tail.htm") ) ); /* Read in the lines of the tail file and copy them into the main HTML file. */ while((s = tail.readLine()) != null) o.write(s, 0, s.length()); tail.close(); // close the tail file o.close(); // close the HTML file // CREATE THE DATA FILES FOR THE GRAPH DISPLAY APPLET for(int i = 0; i < cols; i++) { // for each of the columns of the table /* Create a new file output stream wrapped in a data output stream for the data file named after the current column heading. */ DataOutputStream d = new DataOutputStream( new FileOutputStream( H[i] + ".dat" ) ); /* For each cell in the current column, write the integer in it to the file. */ for(int j = 0; j < rows; j++) d.writeInt(T[i][j]); d.close(); // close the current data file } } }