Control Characters

 

Introduction.  We will now do a little bit more in order to improve our capabilities for displaying output.  While we will eventually get to far more sophisticated approaches when we focus on GUI development, we need some more basic formatting capabilities for message boxes.  We will make use of something called control characters or escape sequences.

Control characters are usually used to do things like improve spacing, force new lines and force the display of special characters that normally won't display.  The following table contains several of the most common control characters.

 

  Escape Sequence Description
\n new line causes text display to move to a new line
\t tab causes text display to have a tab
\b backspace insertion point moves back one space
\r return insertion point moves back to the beginning of same line
\\ backslash causes a backslash to be displayed  (won't otherwise)
\' single quote causes a single quotation mark to be displayed  (won't otherwise)
\" double quote causes a double quotation mark to be displayed  (won't otherwise)

 

We will rework our previous application for data types to modify the display.  We will introduce another swing component called a textarea that will assist us in gaining more control over the display.  We will also modify the code to initially parse the inputs as floating point numbers so that formatting the results will become a bit more involved.

You should call the following program ControlCharacters.java.

 

/* program to get inputs from a user
to solve an equation of the form
ax + b = c

this also gives the output in a somewhat different
display format making use of control characters */


import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class ControlCharacters
{

public static void main( String args[] )
{

String firstUserEntry, secondUserEntry, thirdUserEntry;
int a, b, c, x;
float floatA, floatB, floatC, floatX;
double doubleA, doubleB, doubleC, doubleX;

JTextArea outputArea = new JTextArea();

// initialize first line of the textarea
outputArea.setText("Data Type\ta\t\tb\t\tc\t\tsolution\n\n\n");

// Prompt user for input
JOptionPane.showMessageDialog( null, "You are about to solve \nan equation of the form ax + b = c \nwhere a, b and c are real numbers",
"Integer Product", JOptionPane.INFORMATION_MESSAGE);
firstUserEntry = JOptionPane.showInputDialog("Please enter a number for a:");
secondUserEntry = JOptionPane.showInputDialog("Please enter a number for b:");
thirdUserEntry = JOptionPane.showInputDialog("Please enter a number for c:");

// convert these string values to floats
floatA = Float.parseFloat(firstUserEntry);
floatB = Float.parseFloat(secondUserEntry);
floatC = Float.parseFloat(thirdUserEntry);

// casting these float values to integers
a = (int) floatA;
b = (int) floatB;
c = (int) floatC;
// computing the integer solution
x = (c - b)/a;
// appending the integer solution in the textarea
outputArea.append("Integer\t" + a + "\t\t" + b + "\t\t" + c + "\t\t" + x +"\n\n");


// computing the floating point solution
floatX = (floatC - floatB)/floatA;
// appending the floating point solution into the textarea
outputArea.append("Float\t" + floatA + "\t\t" + floatB + "\t\t" + floatC + "\t\t" + floatX +"\n\n");

// casting the integer values to doubles
doubleA = (double) floatA;
doubleB = (double) floatB;
doubleC = (double) floatC;
// computing the double precision solution
doubleX = (doubleC - doubleB)/doubleA;
// appending the double precision solutions to the textarea
outputArea.append("Double\t" + doubleA + "\t" + doubleB + "\t" + doubleC + "\t" + doubleX +"\n\n");

// displaying the double precision solution
JOptionPane.showMessageDialog( null, outputArea, "Solutions Based on Data Type", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

} // end method main

} // end class ControlCharacters

 

This program has our usual basic structure for applications.  Rather than going over this again I will focus on the code which can be divided into five sections.
  • initial description, imports and declarations
    • notice the extra import line for JTextArea in the swing package
    • notice the the line declaring outputArea as a JTextArea
  • obtaining inputs from the user
    • parsing them to floats
  • casting, computing and appending the integer solution to the textarea
  • computing and appending the floating point solution to the textarea
  • casting, computing and appending the double precision solution to the textarea

But before we discuss the code in more detail, you should follow the usual steps of

  1. move to the directory containing DataCasting.java
  2. javac DataCasting.java
  3. java DataCasting

Then you should try this program with a variety of input values, but in particular use

a = 3.56,  b = 27.34,  c = 65.34

The solution is displayed in the following image.

 

 

Most of the code is the same as before except
  • the message box containing the description of the problem contains new line characters
    • JOptionPane.showMessageDialog( null, "You are about to solve \nan equation of the form ax + b = c \nwhere a, b and c are real numbers",
      "Integer Product", JOptionPane.INFORMATION_MESSAGE);
      • each of the \n causes a new line to appear in the message
  • we parse the inputs to be floating point numbers
  • we initialize the textarea to have a header with
    • outputArea.setText("Data Type\ta\t\tb\t\tc\t\tsolution\n\n\n");
      • Data Type
      • then one tab to the a
      • then two tabs to the b
      • then two tabs to the c
      • then two tabs to the solution
      • we put in three new lines before new text appears
  • we append the solutions of each type to the textarea while adding a couple new lines for spacing afterwards
    • outputArea.append("Integer\t" + a + "\t\t" + b + "\t\t" + c + "\t\t" + x +"\n\n");
      • is a mixture of tab sequences and variables that mimics the header in its spacing to display the integer inputs and solution
    • outputArea.append("Float\t" + floatA + "\t\t" + floatB + "\t\t" + floatC + "\t\t" + floatX +"\n\n");
      • is a mixture of tab sequences and variables that mimics the header in its spacing to display the floating point inputs and solution
    • outputArea.append("Double\t" + doubleA + "\t" + doubleB + "\t" + doubleC + "\t" + doubleX +"\n\n");
      • is a mixture of fewer tab sequences and variables that mimics the header in its spacing to display the floating point inputs and solution
      • we use fewer tabs because the precision that is displayed is so much greater

Much more will be said about this program in class.

It should require much effort to convert this program to an applet in the usual ways.