// File: UsefulIOSubunit.java // // Author: Rahul Simha // Created: Aug 18, 1998 // // Illustrates packages package useful_io_pack; import java.io.*; import java.util.*; public class UsefulIOSubunit { // 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; } }