3.2.3.3 String Conversion3.2.3.2 File Input3.2.3 Text Input3.2.3.4 String Tokenization

3.2.3.3 String Conversion

A BufferedReader stream can be used to read a sequence of text lines each of which is represented in Java as an object of type String. However, this object is frequently just textual representation of a value that has a more usable representation in Java. For instance, the String

   235

read from a file is the text representation of the int value 235. We therefore have to convert the String to the corresponding type.

The Integer class provides for this purpose the method  

   public static int parseInt(String s) 
     throws NumberFormatException

This method returns the int value described by the argument string (in base 10 representation) respectively throws an exception if the string does not describe an integer (no leading or trailing blanks are allowed in the string). The method can be used to return the int value i of a String s as follows:

   int i = 0;
   try
   {
     i = Integer.parseInt(s);
   }
   catch(NumberFormatException e)
   {
     System.out.println("No integer denoted by string " + s);
   }

Similar methods exist for the other numerical datatypes in the classes Byte, Short, Long, Float, Double.

Example  The class Input that we have used up to now for line-oriented input is organized as follows:

   import java.io.*;
   import java.util.*;

   public class Input
   {
     // last read has returned end of stream
     private static boolean endFlag = false;
     
     // message describing the error
     private static String errorMessage = null; 

     // buffered reader wrapped around standard input stream
     private static BufferedReader stdReader = 
       new BufferedReader(new InputStreamReader(System.in));
     
     // current reader
     private static BufferedReader reader = stdReader;
     ...
   }

 

We create a BufferedReader object by wrapping the standard input stream System.in. This object is used by a private method readLine to return the next line of input:

   private static String readLine()
   {
     try
     {
       String line = reader.readLine();
       if (line == null) 
         endFlag = true;
       return line;
     }
     catch(Exception e)
     {
       errorMessage = e.getMessage();
     }
     return null;
   }

This method catches any exception raised by reader.readLine and saves the exception text in a variable errorMessage. If a null line is returned, the endFlag is set to true. The methods isOkay and hasEnded check for these variables as follows:

   public static boolean isOkay()
   {
     return !endFlag && errorMessage == null;
   }

   public static boolean hasEnded()
   {
     return endFlag;
   }

The method getError returns the actual text of the error message.

   public static String getError()
   {
     return errorMessage;
   }

The method clearError resets the error variable:

   public static void clearError()
   {
     errorMessage = null;
   }

The internal method readLine can be immediately used to return a string to the caller:

   public static String readString()
   {
     return readLine();
   }

However, for returning say a int value, the text line has to be correspondingly converted:  

   public static int readInt()
   {
     String line = readLine();
     if (line == null) return Integer.MIN_VALUE;
     try
     {
       return Integer.parseInt(line);
     }
     catch(Exception e)
     {
       errorMessage = e.getMessage();
     }
     return Integer.MIN_VALUE;
   }

Please note that the last return statement is only reached in the case that an exception has been thrown by the conversion.

To set the input source to a text file, the method openInput can be called:  

   public static void openInput(String name)
   {
     try
     {
       reader = new BufferedReader(new FileReader(name));
     }
     catch(FileNotFoundException e)
     {
       errorMessage = e.getMessage();
     }
   }

This method sets the reader object to a new BufferedReader that is connected to a FileReader stream. When closeInput is called, the reader object is reset to the originally opened stdReader.  

   public static void closeInput()
   {
     if (reader == stdReader) 
     {
       errorMessage = "No file has been opened!";
       return;
     }
     try
     {
       reader.close();
     }
     catch (IOException e)
     {
       errorMessage = e.getMessage();
     }
     reader = stdReader;
     endFlag = false;
   }

In this or a similar way, the Java mechanisms for input can be used to implement higher-level abstractions.


© Wolfgang Schreiner; February 3, 2005

3.2.3.3 String Conversion3.2.3.2 File Input3.2.3 Text Input3.2.3.4 String Tokenization