import java.io.*;
import java.util.*;

// Adapted from "Simply Java Programming"
// by Deitel and Deitel, page 820
// Updated: Thursday 27 September 2007, 7:57 PT

// Note - this file has been documented for javadoc
// see www.annedawson.com/javadoc.htm for details




/**
 This is a helper class for the Speaker class.
 It provides simple console input of a string, one input
 value per line. Also provides simple console input of a 
 character. If the user enters an improper input, i.e., 
 a blank line, then the user is prompted to reenter
 the input and given a brief explanation of what is required. 
*/
public class SpeakerHelper
{







    /**
     Reads a line of text and returns that line as a String
     value. The end of a line must be indicated either by a
     new-line character '\n' or by a carriage return '\r'
     followed by a new-line character '\n'. (Almost all systems
     do this automatically. So you need not worry about this
     detail.) Neither the '\n', nor the '\r' if present, are
     part of the string returned. This will read the rest of a
     line if the line is already partially read.
    */
    public static String readLine( )
    {
        char nextChar;
        String result = "";
        boolean done = false;

        while (!done)
        {
            nextChar = readChar( );
            if (nextChar == '\n')
               done = true;
            else if (nextChar == '\r')
            {
                //Do nothing.
                //Next loop iteration will detect '\n'.
            }
            else
               result = result + nextChar;
        }

        return result;
    }










    
    /**
     Reads the next input character and returns that character.
     The next read takes place on the same line where this
     one left off.
    */
    public static char readChar( )
    {
        int charAsInt = -1; //To keep the compiler happy.
        try
        {
            charAsInt = System.in.read( );
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage( ));
            System.out.println("Fatal error. Ending program.");
            System.exit(0);
        }

        return (char)charAsInt;
    }









    /**
     Reads the first nonwhitespace character on a line and
     returns that character. The rest of the line is
     discarded. If the line contains only whitespace, the
     user is asked to reenter the line.
    */
    public static char readLineNonwhiteChar( )
    {
        boolean done = false;
        String inputString = null;
        char nonWhite = ' ';//To keep the compiler happy.

        while (! done)
        {
            inputString = readLine( );
            inputString = inputString.trim( );
            if (inputString.length( ) == 0)
            {
                System.out.println(
                         "Your input is not correct.");
                System.out.println(
                         "Your input must contain at");
                System.out.println(
                         "least one nonwhitespace character.");
                System.out.println("Please try again.");
                System.out.println("Enter input:");
            }
            else
            {
                nonWhite = (inputString.charAt(0));
                done = true;
            }
        }

        return nonWhite;
    }
}
