Converting Strings to Primitive Values


Converting Strings to Primitive Values

We previously explored how other types can be converted into a String . It is sometimes necessary to go the other way, to convert a String into a primitive value. The input data that you use in your programs will be read one of two ways ”as a series of bytes or as character data. If you use character stream I/O you need to convert the input character data into the appropriate type. For example, you may need to convert the String "14" into the primitive integer value 14 .

To accomplish String -to-primitive type conversions, we turn again to the primitive variable wrapper classes. As was discussed in the "The parse() Methods " section earlier in this chapter, there are basically two ways to convert String objects to primitive values. The first way is to use a combination of the valueOf() and <primitive>Value() methods. For example, to convert the String "14" to an integer primitive value and assign that value to an integer variable you might type

 int k = Integer.valueOf("14").intValue(); 

The example is really a compound version of two separate statements ”

 Integer temp = Integer.valueOf("14"); int k = temp.intValue(); 

The second, and more efficient, way to convert a String to a primitive value is by using one of the parse() methods. For example, you can use the parseInt() method to convert the String "14" to a primitive integer value.

 int k = Integer.parseInt("14"); 

Input file data will often be read as a String that contains other characters besides the input data that you need. This is often done to make the input file more readable. For instance, an input file might contain the following ”

 Number of engines = 14 

If you read this entire line into your program as a single String , using the readLine() method of the BufferedReader class for instance, you would need to extract the substring "14" from the remainder of the String . The split() method from the String class can be used to divide a String into substrings based on a regular expression that will serve as the delimiter . In this case, the regular expression would be set to = and the String 14 would be the last element of the resulting substring array.

Example: Parsing an Input File

This example shows how String objects and the primitive variable wrapper classes can work together to parse data read from an input file. The input file, parse.inp, contains the following three lines:

 configuration = 2D width = 2.0 height = 3.0 

When we read the lines of this input file, we are only interested in the parts after the equals sign. The data will be read in line by line as Strings . We need to extract the part of each String after the equal sign, and we need to convert the 2.0 and 3.0 substrings into primitive values.

BufferedReader and FileReader objects are used to connect a character input stream to the input file. The first line of the input file describes the geometry type of the problem. A 2-D rectangle will have a width and a height. The readLine() method reads the first line of the input file in as a String . This String is then divided into substrings with the split() method using the = character as the delimiter. The last element of the resulting String array is the configuration data that we want.

The equals() method is used to check if the String assigned to the config variable is equal to the String "2D." If it is, then the program reads the next two lines of the input file and extracts the width and height data from the appropriate Strings . Once again, the split() method is used to split the Strings into substrings using = as the regular expression to search for. The String representation of the numerical values we want will be the last element of the strings[] array. The parseDouble() method from the Double class is used to return the primitive double value associated with the last substring. Just to check that everything worked out properly, the product of the width and height values is printed.

 import java.io.*; public class ParseDemo {   public static void main(String args[]) {     String data;     String strings[];     String config;     double width=0.0, height=0.0;     // Connect an input stream with an input file     try{       BufferedReader reader =           new BufferedReader(               new FileReader("parse.inp"));       //  Read the first line of the input file.  This       //  will have the configuration information.  The       //  String is split into substrings.  The last       //  substring is the one we want to keep after any       //  leading and trailing white space is trimmed.       data = reader.readLine();       strings = data.split("=");       config = strings[strings.length-1].trim();       //  If the config String equals "2D", read the       //  next two lines from the input file, split the       //  resulting Strings into substrings, and use the       //  parseDouble() method to extract the desired       //  double value. Note that "length" is the array       //  length field not the length() method of the       //  String class.       if ( config.equals("2D") ) {         data = reader.readLine();         strings = data.split("=");         width =           Double.parseDouble(strings[strings.length-1]);         data = reader.readLine();         strings = data.split("=");         height =           Double.parseDouble(strings[strings.length-1]);       } else {         System.out.println("not 2D");       }       reader.close();     } catch (IOException ioe) {       System.out.println("IOException: "+ioe);     }     System.out.println("area is "+width*height);   } } 

Output ”

 area is 6.0 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net