A Slightly Friendlier I/O Toolkit
There are lots of ways to get input and output in any given program. One way other than using the Java System operations is to use a set of tools developed for this reference. These tools were developed for learning the material in this reference and you will not find them anywhere else. Using these tools at this point in your learning is not critical but it does offer both ability to make nice-looking displays and it gives you plenty of opportunity to learn more about using methods.
Consider one of the previous examples, which was the code required to prompt and input data. The process looked like the following.
strInput = JOptionPane.showInputDialog( "Enter your birth year: " );
intInput = Integer.parseInt( strInput );
If you use the formatted command line I/O tool set called Console_IO_Class, the following would replace the JOptionPane prompt and parseInt conversion code.
birthYear = conIO.promptForInt( "Enter your birth year: " );
You will need to instantiate the conIO object before you can use this tool. For right now, you can place it under the initialize program part of your program, shown below. Later, as the programs get a little more complicated and you learn about scope, you may find yourself moving it to a different location:
public static void main( String[] args )
{
// initialize program
// initialize the variables
Console_IO_Class conIO = new Console_IO_Class();
The process of prompting and inputting are packaged into a method, which makes your code more readable, and simpler. There is a promptFor method for all data types you will commonly use (and about which you will learn later in this reference).
In addition, there are output methods that allow you to output formatted data. For example, a simple output would look like the following.
conIO.printInt( age );
This has just one parameter because the age variable is needed for the printInt method to do its job. However, this method is called an overloaded method because there is another one with the same name but different parameters. The other method has three parameters, which include the data to be output, the size of a block to output it in, and the justification of the output.
The overloaded method can also set an outputted quantity in a formatted output. The following shows an example of the power of this tool.
precision = 2;
blockSize = 10;
age = 19;
conIO.printChar( PIPE );
conIO.printInt( age, blockSize, "LEFT" );
conIO.printChar( PIPE );
This method is a little different from the previous one which simply output the age to the console. In this one, the age is placed in a left justified form within a block of characters. This example includes printed pipes (i.e., '|') so that you can see what happens. Here is the output for the above code.
|19 |
A pipe is printed, then the age is printed on the left side of the block (of ten characters), then spaces are made so that the whole block is 10 characters wide (i.e., the block size, which is between the pipes), and finally, another pipe is printed so you can see the block.
Another overloaded method is printDouble which prints double of floating point (i.e., numbers with a decimal point). The overloaded printDouble method can also set an outputted quantity in a formatted output. The following shows an example of the power of this tool.
precision = 2;
blockSize = 10;
payRate = 3.4567;
conIO.printChar( PIPE );
conIO.printDouble( payRate, precision, blockSize, "LEFT" );
conIO.printChar( PIPE );
This method is a little different from printInt. Since it is a double output, it also includes a parameter for the number of digits following the decimal point, which is somewhat incorrectly, but commonly, called precision. This example includes printed pipes (i.e., '|') so that you can see what happens. Here is the output for the above code.
|3.46 |
A pipe is printed, then the number with two digits (rounded) after the decimal is printed on the left side of the block, then spaces are made so that the whole block is 10 characters wide (i.e., the block size), and finally, another pipe is printed so you can see the block.
Here is another example with right justification.
precision = 2;
blockSize = 10;
payRate = 3.4567;
conIO.printChar( PIPE );
conIO.printDouble( payRate, precision, blockSize, "RIGHT" );
conIO.printChar( PIPE );
This time, the data is printed at the right side of the 10-character block. Again, the pipes are printed to show you the effect.
| 3.46|
There are several possibilities for inputting and outputting using these tools. To learn more, just look at the code in the Console_IO_Class.java file. There are several commented specifications that can help you choose and use the appropriate methods to make nice-looking displays.
Watch this series of videos: Console I/O Step One, Console I/O Step Two, Console I/O Step Three, Console I/O Step Four to see what you can do with this tool. The series implements a complete input and formatted output program using the Four Step Programming Process that will significantly exercise your linear progression and method learning. In addition, you are introduced to variables and global constants. Make sure you work through the code yourself and try some different actions. And the more times you work through the video and the code, the stronger your foundation will be.