// File: read_geodata2.java // // Author: Rahul Simha // Created: Aug 18, 1998 // // Same as solutions/read_geo_file.java except // that some useful functions are encapsulated. import java.io.*; import java.util.*; class useful_io { // Read a string from the screen. static String read_string (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 ("gwu_io::read_string: cannot read\n"); System.exit (0); return ""; } } // Parse a string for a property. public static double read_property (String in_string, String property) { StringTokenizer st = new StringTokenizer (in_string); String first_part = st.nextToken ("="); first_part = first_part.trim (); if (!first_part.equals (property)){ System.out.println ("Improper string: " + in_string); System.exit (0); } String second_part = st.nextToken (" =\t\n\r"); second_part = second_part.trim(); try { Double d = Double.valueOf (second_part); return d.doubleValue(); } catch (NumberFormatException e) { System.out.println ("Second part not a number: " + second_part); System.exit (0); } return 0; } } // End of class useful_io public class read_geodata2 { public static void main (String[] argv) { // Get file name String filename = useful_io.read_string ("Enter filename: "); try { FileReader fr = new FileReader (filename); LineNumberReader lr = new LineNumberReader (fr); // Now read the input lines boolean over = false; int n_lines = 0; do { String input_line = lr.readLine (); if (input_line != null) { System.out.println ("Line " + (++n_lines) + ": " + input_line); input_line = input_line.trim (); if (input_line.equals ("Circle:")) { // Circle input_line = lr.readLine (); double cx = useful_io.read_property (input_line, "center.x"); input_line = lr.readLine (); double cy = useful_io.read_property (input_line, "center.y"); input_line = lr.readLine (); double r = useful_io.read_property (input_line, "radius"); double area = 3.14159 * r * r; System.out.println ("Circle: cx=" + cx + " cy=" + cy + " r=" + r + " area=" + area); } else if (input_line.equals ("Rectangle:")) { // Rectangle input_line = lr.readLine (); double tx = useful_io.read_property (input_line, "topleft.x"); input_line = lr.readLine (); double ty = useful_io.read_property (input_line, "topleft.y"); input_line = lr.readLine (); double bx = useful_io.read_property (input_line, "bottomright.x"); input_line = lr.readLine (); double by = useful_io.read_property (input_line, "bottomright.y"); double area = (bx - tx) * (ty - by); System.out.println ("Rectangle: tx=" + tx + " ty=" + ty + " bx=" + bx + " by=" + by + " area=" + area); } else if (input_line.equals ("Square:")) { // Square input_line = lr.readLine (); double tx = useful_io.read_property (input_line, "topleft.x"); input_line = lr.readLine (); double ty = useful_io.read_property (input_line, "topleft.y"); input_line = lr.readLine (); double side = useful_io.read_property (input_line, "side"); double area = side * side; System.out.println ("Square: tx=" + tx + " ty=" + ty + " area=" + area); } else { System.out.println ("Unrecognizable: " + input_line); } } else over = true; } while (! over); // Close the file. lr.close(); } catch (IOException e) { System.out.println (e); } } } // End of read_geodata2