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 {
} // 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.
But before we discuss the code in more detail, you should follow the usual steps of
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
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. |