|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
Example:
// The following method call is platform-dependent because of the \n character.
System.out.print ("ABC\nDEF");
// The following method calls are platform-independent.
System.out.println ("ABC");
System.out.print ("DEF");
The standard error device is that part of the operating system that controls where a program sends error messages. Unlike the standard output device,
you cannot redirect standard error on platforms such as Windows 98 SE. Use System.err instead of System.out to distinguish standard error from standard output:
Example:
// Output the following error message to standard error.
System.err.println ("usage: java copy srcfile dstfile");
It's a good idea to use System.err instead of System.out for outputting error messages. After all, a user might become confused if a problem occurs in a program and no error message
appears on the screen because the message is sent to the standard output device -- which has been redirected from the screen
to a file.
You might wonder why these introductory Java 101 articles don't use GUIs to solicit input and display output. The reason is because Java's GUI framework is more complex than standard I/O, and a complete discussion of this framework reaches far beyond the scope of introductory Java 101 articles. However, several articles from now, you will explore this GUI framework, and your reliance on standard I/O for a program's input and output tasks will diminish.
Now that you've explored the concepts of standard input, standard output, and standard error (as well as several simple programs and code fragments that demonstrate these concepts), let's create a useful program that exploits these concepts.
Suppose you want to know how many words an article that you wrote contains. Instead of counting by hand, implement a utility program that counts words for you. Before writing the utility, you need to decide what constitutes a word. After all, it is pointless to write a word-counting program if you don't know how to tell the program how to interpret a word. My definition of a word is a sequence of letters -- either uppercase or lowercase. You could change that definition to include hyphen characters, if desired.
Listing 5 presents the source code to WordCount -- a word-counting program that I've put together. Like myself, you might find this program to be useful.
Listing 5. WordCount.java
// WordCount.java
import java.io.*;
class WordCount
{
static int nWords;
public static void main (String [] args) throws IOException
{
int ch;
// Read each character from standard input until a letter
// is read. This letter indicates the start of a word.
while ((ch = System.in.read ()) != -1)
{
// If character is a letter then start of word detected.
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
{
do
{
ch = System.in.read ();
if (ch >= 'A' && ch <= 'Z' ||
ch >= 'a' && ch <= 'z' ||
ch >= '0' && ch <= '9')
continue;
else
break;
}
while (true);
nWords++;
}
}
System.out.println ("\nTotal words = " + nWords);
}
}
WordCount is pretty straightforward, except for the throws java.io.IOException clause appended to the main() method header. You must include the clause because System.in.read can throw, or pass, an exception -- an object that describes a problem -- to the JVM. (You will explore exceptions in detail in a future article.)