import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

/**
 * An application that reads text from the console,
 * counts the number of words that start with uppercase
 * letters, and saves the text in a file.
 *
 * This version is not cohesive
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class SillyTextProcessor
{

    /**
     * The entry-point of the application
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args) throws Exception
    {
       BufferedReader      in;
       int                 lines, maxLines, ucCount;
       JFrame              frame;
       JProgressBar        bar;
       PrintWriter         out;
       String              letter, letterUC, line, word;
       StringTokenizer     tokenizer;



       // Process the command line parameter
       try 
       {
          maxLines = Integer.parseInt(args[0]);
       }
       catch (NumberFormatException nfe) 
       {
          maxLines = 5;
       }

       // Initialization
       in       = new BufferedReader(new InputStreamReader(System.in));
       out      = new PrintWriter(new FileOutputStream("archive.txt"));
       bar      = new JProgressBar(0, maxLines);
       frame    = new JFrame();

       bar.setString("Lines Read");
       bar.setStringPainted(true);
       frame.getContentPane().add(bar);
       frame.setSize(300,50);
       frame.setVisible(true);
	

//[LineReader1.

       // Prompt the user
       System.out.println("Enter " +maxLines +
                          " lines of text (^Z to end):\n");

       // Read from the console
       lines = 0;
       while ((line = in.readLine()) != null) 
       {
          // Process each line
//]LineReader1.

//[ProgressBar.

          // Indicate the progress
          ++lines;
          bar.setValue(lines);
//]ProgressBar.

//[UCWordCounter.

          // Count the number of words that start with
          // uppercase characters (using Java's 
          // definition of uppercase)
          tokenizer = new StringTokenizer(line);
          ucCount = 0;
          while (tokenizer.hasMoreTokens()) 
          {
             word = tokenizer.nextToken();
             letter = word.substring(0,1);
             letterUC = letter.toUpperCase();
             if (letter.equals(letterUC)) ++ucCount;
          }
          System.out.println("Start with uppercase: "+ucCount);
//]UCWordCounter.


//[LineArchiver.

          // Save the text in a file
          out.println(line);
//]LineArchiver.

//[LineReader2.

       }
//]LineReader2.

       out.flush();
       out.close();
    }

}
