// TestASCII.java
/**
* Program which tests whether a specified
* input file contains only ASCII characters.
*
* @author D. Nixon
*/
public class TestASCII {
/**
* Main method.
* @param args command-line arguments.
* One command-line argument needed
* to specify input text file.
*/
public static void main(String[] args)
{
if ( args.length == 0 )
{
// Announce what this program will do:
System.out.println("This program will determine whether the");
System.out.println("file specified as a command-line argument");
System.out.println("contains only ASCII characters.");
System.exit(0);
} // if
// Prepare to read from input text file:
TextFileInput dataFile = new TextFileInput(args[0]);
// No lines containing non-ASCII characters have been found YET:
boolean nonAsciiFound = false;
// Check each line for non-ASCII characters:
while ( true )
{
String line = dataFile.readLine();
if ( line == null )
break;
if ( containsNonAscii(line) )
{
nonAsciiFound = true;
break;
} // if
} // while
System.out.println("The file " + args[0] + " is "
+ (nonAsciiFound ? "NOT " : "")
+ "an all-ASCII file.");
} // method main
/**
* Determines whether the specified string
* contains at least one non-ASCII character.
*
* @param text the string to be checked
* for non-ASCII characters
* @return true if <code>text</code> contains
* at least one non-ASCII character,
* false otherwise
*/
private static boolean containsNonAscii(String text)
{
for ( int i = 0; i < text.length(); i++ )
if ( text.charAt(i) > 127 )
return true;
return false;
} // method containsNonAscii
} // class TestASCII