// File: ReadGeoData.java // // Author: Rahul Simha // Created: Aug 18, 1998 import java.io.*; import java.util.*; class UsefulIO { // Read a string from the screen. public static String readString (String prompt) { System.out.print (prompt); InputStreamReader isr = new InputStreamReader (System.in); LineNumberReader lr = new LineNumberReader (isr); try { String s = lr.readLine (); return s; } catch (IOException e) { System.out.println ("UsefulIO::readString: cannot read\n"); System.exit (0); return ""; } } // Parse a string for a property. public static double readProperty (String inString, String property) { StringTokenizer st = new StringTokenizer (inString); String firstPart = st.nextToken ("="); firstPart = firstPart.trim (); if (!firstPart.equals (property)){ System.out.println ("Improper string: " + inString); System.exit (0); } String secondPart = st.nextToken (" =\t\n\r"); secondPart = secondPart.trim(); try { Double d = Double.valueOf (secondPart); return d.doubleValue(); } catch (NumberFormatException e) { System.out.println ("Second part not a number: " + secondPart); System.exit (0); } return 0; } } // End of class UsefulIO public class ReadGeoData { public static void main (String[] argv) { // Get file name String filename = UsefulIO.readString ("Enter filename: "); try { FileReader fr = new FileReader (filename); LineNumberReader lr = new LineNumberReader (fr); // Now read the input lines boolean over = false; int numLines = 0; do { String inputLine = lr.readLine (); if (inputLine != null) { System.out.println ("Line " + (++numLines) + ": " + inputLine); inputLine = inputLine.trim (); if (inputLine.equals ("Circle:")) { // Circle inputLine = lr.readLine (); double cx = UsefulIO.readProperty (inputLine, "center.x"); inputLine = lr.readLine (); double cy = UsefulIO.readProperty (inputLine, "center.y"); inputLine = lr.readLine (); double r = UsefulIO.readProperty (inputLine, "radius"); double area = 3.14159 * r * r; System.out.println ("Circle: cx=" + cx + " cy=" + cy + " r=" + r + " area=" + area); } else if (inputLine.equals ("Rectangle:")) { // Rectangle inputLine = lr.readLine (); double tx = UsefulIO.readProperty (inputLine, "topleft.x"); inputLine = lr.readLine (); double ty = UsefulIO.readProperty (inputLine, "topleft.y"); inputLine = lr.readLine (); double bx = UsefulIO.readProperty (inputLine, "bottomright.x"); inputLine = lr.readLine (); double by = UsefulIO.readProperty (inputLine, "bottomright.y"); double area = (bx - tx) * (ty - by); System.out.println ("Rectangle: tx=" + tx + " ty=" + ty + " bx=" + bx + " by=" + by + " area=" + area); } else if (inputLine.equals ("Square:")) { // Square inputLine = lr.readLine (); double tx = UsefulIO.readProperty (inputLine, "topleft.x"); inputLine = lr.readLine (); double ty = UsefulIO.readProperty (inputLine, "topleft.y"); inputLine = lr.readLine (); double side = UsefulIO.readProperty (inputLine, "side"); double area = side * side; System.out.println ("Square: tx=" + tx + " ty=" + ty + " area=" + area); } else { System.out.println ("Unrecognizable: " + inputLine); } } else over = true; } while (! over); // Close the file. lr.close(); } catch (IOException e) { System.out.println (e); } } } // End of ReadGeoData2