// File: FileIO4.java // // Author: Rahul Simha // Created: August 18, 1998. // // A useful function to extract information from // property files. import java.util.*; public class FileIO4 { // Parameters: // inString: the property string (e.g. "radius=0.9") // property: the property name (e.g., "radius") // The value is assumed to be a real number. public static double readProperty (String inString, String property) { // Obtain a copy of the StringTokenizer object. StringTokenizer st = new StringTokenizer (inString); // Get the first token, specifying the delimiter. String firstPart = st.nextToken ("="); // Trim whitespace on either side - a String function. firstPart = firstPart.trim (); System.out.println ("First part: " + firstPart); if (!firstPart.equals (property)){ System.out.println ("Improper string: " + inString); System.exit (0); } // Get the next token, now allowing for end-of-line. String secondPart = st.nextToken (" =\t\n\r"); // Trim whitespace. secondPart = secondPart.trim(); System.out.println ("Second part: " + secondPart); // Read the number in the second part using the library's // Double object. try { double d = Double.parseDouble (secondPart); return d; } catch (NumberFormatException e) { System.out.println ("Second part not a number: " + secondPart); System.exit (0); } return 0; } public static void main (String[] argv) { // Test double d = readProperty ("x=5", "x"); System.out.println (d); d = readProperty ("center.x=5", "center.x"); System.out.println (d); d = readProperty ("center.x=5 ", "center.x"); System.out.println (d); d = readProperty (" center.x = 5 ", "center.x"); System.out.println (d); } }