/** * Converts the table in domfin.txt to separate .dat files: one for each column. * @author Robert J Morton * @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 COLS = 46, // total number of columns in table ROWS = 36, // number of rows in table including heading row cols = COLS - 2; // number of columns excluding Age & Year columns int rows = ROWS - 1; // number of rows excluding the title row /* 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]; /* For numeric values in body of table. These are multiplied up to in- tegral pence except for the kWh columns which are left as they are. */ int T[][] = new int[cols][rows]; String s; // to hold an imput line of text /* Open the html output file and wrap it in an output stream writer and wrap that in a buffered writer in order to be able to 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 in order to be able to use readLine(). */ BufferedReader head = new BufferedReader( new InputStreamReader( new FileInputStream("head.htm") ) ); while((s = head.readLine()) != null) // read in next line of head file o.write(s,0,s.length()); // copy it into the main HTML file head.close(); // close the head file /* 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 in order to be able to 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 is q = s.length(); // found, then end tab is string length h = s.substring(p, q); // extract the current column heading if(i > 1) // store all but the first two column H[i - 2] = h; // headingds in the array // write the text of the HTLM column heading h = ""; o.write(h,0,h.length()); 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) { /* Clear start and end tab positions for the column then write the HTML row start tag. */ p = 0; q = 0; o.write("",0,18); // for each of the columns of this line for(int i = 0; i < COLS; i++) { /* if no [further] tab character is found, then the end tab is the string length. */ if((q = s.indexOf(c, p)) == -1) q = s.length(); h = s.substring(p, q); // extract the current column string if(i > 1) { // skip the age and year columns 0 and 1 int x = 0; // integer value of column in integral pence try { /* Convert the substring containing the numeric string of the currrent column to a Double-precision floating-point OBJECT. */ Double d = Double.valueOf(s.substring(p, q)); /* Convert the double object 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.*/ x = (int)Math.round(d.doubleValue() * 100); } /* If sub-string in this column won'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 first two columns (Age & Year) are not included. */ T[i - 2][row] = x; first } h = ""; // form the HTML table column o.write(h, 0, h.length()); // write it to the HTML file p = q + 1; // Next time's start position = 1 + this times end position } 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(); // end of HTML line o.write(""); o.newLine(); // end of HTML line /* Open the input table file and wrap it in an input stream reader and wrap that in a buffered reader in order to be able to use readLine(). */ BufferedReader kt = new BufferedReader( new InputStreamReader( new FileInputStream("tabkey.txt") ) ); // for each of the table's column headings for(int i = 0; i < cols; i++) { try { s = kt.readLine(); // get its explanation line from the file } catch(IOException e) { // if explanation not present s = "[unspecified]"; // give it as unspecified } 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 in order to be able to use readLine().*/ BufferedReader tail = new BufferedReader( new InputStreamReader( new FileInputStream("tail.htm") ) ); /* Read in the next line of the head file and copy it 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. Then for each cell in the current column write the integer in it to the file Then close the file. */ DataOutputStream d = new DataOutputStream( new FileOutputStream(H[i] + ".dat") ); for(int j = 0; j < rows; j++) d.writeInt(T[i][j]); d.close(); } } }