Please send questions to
st10@humboldt.edu .
/**
/ just playing around with "terminal" input; uses terminal I/O as used in
/ example FileCopy.java (itself from Flanagan's "Java Examples in
/ a Nutshell", FIRST EDITION, O'Reilly)
/
/ simply "parrot" back every line the user enters to the screen, until
/ the user types "HUSH!" to make it stop.
/
/ modified by: Sharon M. Tuttle
/ last modified: 4-9-01
**/
import java.io.*;
public class Parrot
{
public static void main(String[] args)
{
BufferedReader inBuffReader;
String userLine;
// being kind --- telling the user how to escape...
System.out.println("Polly wants to parrot everything you type in ---");
System.out.println(" but Polly will stop if you enter the line HUSH!");
System.out.println();
System.out.flush();
// set up a BufferedReader to read from the terminal (System.in)
// (this BufferedReader() constructor expects a Reader instance
// as an argument --- this InputStreamReader() constructor
// can accept InputStream System.in to provide a Reader instance.)
inBuffReader = new BufferedReader(
new InputStreamReader(System.in));
// get the first line typed in by the user;
// (system made me put this in a try-catch block,
// by the way --- this can throw an IOException;)
try
{
userLine = inBuffReader.readLine();
// has user asked you to HUSH yet? If not, "parrot" the
// response back to the screen, and keep going until asked
// to stop;
while (!userLine.equals("HUSH!"))
{
// "parrot" latest user response back to the screen
System.out.println(userLine);
System.out.println(); // extra blank line, to make output "prettier"
// get next line
userLine = inBuffReader.readLine();
}
}
catch (IOException exc)
{
System.out.println(exc.getMessage());
}
// whether I get here because HUSH! was entered, or because
// of a readLine()-thrown IOException --- I think closing the
// BufferedReader and exiting ought to be a reasonable
// reaction.
finally
{
// close inBuffReader and leave --- yeah, IT can throw an
// IOException, too...!
System.out.println("Polly says 'bye, now...");
try
{
inBuffReader.close();
}
catch (IOException exc)
{
System.out.println(exc.getMessage());
}
System.exit(0);
}
}
}