// CountOddFile2.java
public class CountOddFile2 {
public static void main(String[] args)
{
if ( args.length == 0 )
{
// Announce what this program will do:
System.out.println("This program will count the odd numbers "
+ "in a text file specified as a");
System.out.println("Command-line argument. The file must "
+ "contain a column of integers.");
System.exit(0);
} // if
// Prepare to read from input text file:
TextFileInput dataFile = new TextFileInput(args[0]);
// Initialize cumulative counts:
int countAll = 0;
int countOdd = 0;
while ( true )
{
// Input a line of text from the file:
String line = dataFile.readLine();
if ( line == null )
break;
// Convert the line to an integer
// only if the line exists:
int number = Integer.parseInt(line);
// If the number is odd, increment
// count of odd numbers:
if ( number % 2 == 1 )
countOdd++;
// Increment count of all numbers in file:
countAll++;
} // while
System.out.println("There are " + countOdd
+ " odd numbers in the file "
+ args[0] + ", which contains "
+ countAll + " numbers total.");
} // method main
} // class CountOddFile2