//FileRead.java //robert j. russell import java.io.*; public class FileRead { FileReader fstream; BufferedReader in; String lastLine; boolean lineRead; public FileRead(String name) { lineRead = false; lastLine = ""; try { fstream = new FileReader(name); in = new BufferedReader(fstream); } catch (IOException e) { System.out.println("File not found: "+e.getMessage()); System.exit(-2); } } public void close() { try { in.close(); } catch (Exception e) { } } public boolean eof() { if (lineRead) return false; try { if (!in.ready()) { return true; } } catch (Exception e) { return true; } lastLine = getString(); lineRead = true; return false; } public int getInt() { String input = getString(); String rest = input+""; input = input.trim(); char ch = input.charAt(0); if ((ch < '0' || ch > '9') && ch != '+' && ch != '-') { System.out.println("Invalid Integer: " + input); return 0; } int pos = input.indexOf(' '); if (pos > 0) { rest = input.substring(0, pos); input = input.substring(pos+1); lastLine = input.trim(); if (lastLine.length() > 0) lineRead = true; else lineRead = false; } int rlt; try { rlt = Integer.parseInt(rest); } catch (Exception e) { System.out.println("Invalid Integer: " + rest); return 0; } return rlt; } public double getDouble() { String input = getString(); String rest = input+""; input = input.trim(); char ch = input.charAt(0); if ((ch < '0' || ch > '9') && ch != '+' && ch != '-') { System.out.println("Invalid Double: " + input); return 0.0; } int pos = input.indexOf(' '); if (pos > 0) { rest = input.substring(0, pos); input = input.substring(pos+1); lastLine = input.trim(); if (lastLine.length() > 0) lineRead = true; else lineRead = false; } double rlt; try { rlt = Double.parseDouble(rest); } catch (Exception e) { System.out.println("Invalid Double: " + input); return 0; } return rlt; } public String getString() { if (lineRead) { lineRead = false; return lastLine; } try { if (in.ready()) { return in.readLine(); } return ""; } catch (Exception e) { System.out.println("An error occured in getString()"); return ""; } } }