Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TextIO

807601Jul 2 2008 — edited Jul 2 2008
So in a tutorial I'm using to learn Java it uses a class file called TextIO that provides easy basic text input and output (hence the name). But whenever I run it, it returns a bunch of errors I don't know the meaning of. Can somebody help me figure how to get this to work?
/**
* This class implements a simple program that will compute
* the amount of interest that is earned on an investment over
* a period of one year.  The initial amount of the investment
* and the interest rate are input by the user.  The value of
* the investment at the end of the year is output.  The
* rate must be input as a decimal, not a percentage (for
* example, 0.05 rather than 5).
*/

public class Interest2 {

   public static void main(String[] args) {
   
       double principal;  // The value of the investment.
       double rate;       // The annual interest rate.
       double interest;   // The interest earned during the year.
       
       TextIO.put("Enter the initial investment: ");
       principal = TextIO.getlnDouble();
       
       TextIO.put("Enter the annual interest rate (decimal, not percentage!): ");
       rate = TextIO.getlnDouble();
       
       interest = principal * rate;       // Compute this year's interest.
       principal = principal + interest;  // Add it to principal.
       
       TextIO.put("The value of the investment after one year is $");
       TextIO.putln(principal);
       
   } // end of main()
      
} // end of class Interest2
import java.io.*;
import java.util.IllegalFormatException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
* TextIO provides a set of static methods for reading and writing text.  By default, it reads
* from standard input and writes to standard output, but it is possible to redirect the input
* and output to files or to other input and output streams.  When the standard input and output
* streams are being used, the input methods will not produce an error; instead, the user is
* repeatedly prompted for input until a legal input is entered.  (If standard input has been
* changed externally, as by file redirection on the command line, this is not a reasonable
* behavior; to handle this case, TextIO will give up after 10 consecutive illegal inputs and
* will throw an IllegalArgumentException.)  For the most part, any other
* error will be translated into an IllegalArguementException.
* <p>For writing to standard output, the output methods in this class pretty much
* duplicate the functionality of System.out, and System.out can be used interchangeably with them.
* <p>This class does not use optimal Java programming practices.  It is designed specifically to be easily
* usable even by a beginning programmer who has not yet learned about objects and exceptions.  Therefore, 
* everything is in a single source file that compiles into a single class file, all the methods are
* static methods, and none of the methods throw exceptions that would require try...catch statements.
* Also for this reason, all exceptions are converted into IllegalArgumentExceptions, even when this
* exception type doesn't really make sense.
* <p>This class requires Java 5.0 or higher. (A previous version of TextIO required only Java 1.1;
* this version should work with any source code that used the previous version, but it has some new
* features, including the type of formatted output that was introduced in Java 5 and the ability to
* use files and streams.)
*/
public class TextIO {

   /* Modified November 2007 to empty the TextIO input buffer when switching from one
    * input source to another. This fixes a bug that allows input from the previous input
    * source to be read after the new source has been selected.
    */

   /**
    * The value returned by the peek() method when the input is at end-of-file.
    * (The value of this constant is (char)0xFFFF.)
    */
   public final static char EOF = (char)0xFFFF; 

   /**
    * The value returned by the peek() method when the input is at end-of-line.
    * The value of this constant is the character '\n'.
    */
   public final static char EOLN = '\n';          // The value returned by peek() when at end-of-line.
   

   /**
    * After this method is called, input will be read from standard input (as it 
    * is in the default state).  If a file or stream was previously the input source, that file
    * or stream is closed.
    */
   public static void readStandardInput() {
      if (readingStandardInput)
         return;
      try {
         in.close();
      }
      catch (Exception e) {
      }
      emptyBuffer();  // Added November 2007
      in = standardInput;
      inputFileName = null;
      readingStandardInput = true;
      inputErrorCount = 0;
   }
   
   /**
    * After this method is called, input will be read from inputStream, provided it
    * is non-null.  If inputStream is null, then this method has the same effect
    * as calling readStandardInput(); that is, future input will come from the
    * standard input stream.
    */
   public static void readStream(InputStream inputStream) {
      if (inputStream == null)
         readStandardInput();
      else
         readStream(new InputStreamReader(inputStream));
   }
   
   /**
    * After this method is called, input will be read from inputStream, provided it
    * is non-null.  If inputStream is null, then this method has the same effect
    * as calling readStandardInput(); that is, future input will come from the
    * standard input stream.
    */
   public static void readStream(Reader inputStream) {
      if (inputStream == null)
         readStandardInput();
      else {
         if ( inputStream instanceof BufferedReader)
            in = (BufferedReader)inputStream;
         else
            in = new BufferedReader(inputStream);
         emptyBuffer();  // Added November 2007
         inputFileName = null;
         readingStandardInput = false;
         inputErrorCount = 0;
      }
   }
   
   /**
    * Opens a file with a specified name for input.  If the file name is null, this has
    * the same effect as calling readStandardInput(); that is, input will be read from standard
    * input.  If an
    * error occurs while trying to open the file, an exception of type IllegalArgumentException
    * is thrown, and the input source is not changed.  If the file is opened 
    * successfully, then after this method is called, all of the input routines will read 
    * from the file, instead of from standard input.
    */
   public static void readFile(String fileName) {
      if (fileName == null) // Go back to reading standard input
         readStandardInput();
      else {
         BufferedReader newin;
         try {
            newin = new BufferedReader( new FileReader(fileName) );
         }
         catch (Exception e) {
            throw new IllegalArgumentException("Can't open file \"" + fileName + "\" for input.\n"
                           + "(Error :" + e + ")");
         }
         if (! readingStandardInput) { // close current input stream
            try {
               in.close();
            }
            catch (Exception e) {
            }
         }
         emptyBuffer();  // Added November 2007
         in = newin;
         readingStandardInput = false;
         inputErrorCount = 0;
         inputFileName = fileName;
      }
   }

   /**
    * Puts a GUI file-selection dialog box on the screen in which the user can select
    * an input file.  If the user cancels the dialog instead of selecting a file, it is
    * not considered an error, but the return value of the subroutine is false.
    * If the user does select a file, but there is an error while trying to open the
    * file, then an exception of type IllegalArgumentException is thrown.  Finally, if
    * the user selects a file and it is successfully opened, then the return value of the
    * subroutine is true, and  the input routines will read from the file, instead of 
    * from standard input.   If the user cancels, or if any error occurs, then the
    * previous input source is not changed.
    * <p>NOTE: Calling this method starts a GUI user interface thread, which can continue
    * to run even if the thread that runs the main program ends.  If you use this method
    * in a non-GUI program, it might be necessary to call System.exit(0) at the end of the main() 
    * routine to shut down the Java virtual machine completely.
    */
   public static boolean readUserSelectedFile() {
      if (fileDialog == null)
         fileDialog = new JFileChooser();
      fileDialog.setDialogTitle("Select File for Input");
      int option = fileDialog.showOpenDialog(null);
      if (option != JFileChooser.APPROVE_OPTION)
         return false;
      File selectedFile = fileDialog.getSelectedFile();
      BufferedReader newin;
      try {
         newin = new BufferedReader( new FileReader(selectedFile) );
      }
      catch (Exception e) {
         throw new IllegalArgumentException("Can't open file \"" + selectedFile.getName() + "\" for input.\n"
                        + "(Error :" + e + ")");
      }
      if (!readingStandardInput) { // close current file
         try {
            in.close();
         }
         catch (Exception e) {
         }
      }
      emptyBuffer();  // Added November 2007
      in = newin;
      inputFileName = selectedFile.getName();
      readingStandardInput = false;
      inputErrorCount = 0;
      return true;
   }
   
   /**
    * After this method is called, output will be written to standard output (as it 
    * is in the default state).  If a file or stream was previously open for output, it
    * will be closed.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 30 2008
Added on Jul 2 2008
14 comments
1,216 views