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