Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Listing 1-1 shows a utility that I use in some of my programs to generate a pause. I use it when I want to run something separately (e.g., a query to some tables being modified by the program), but want to do so at intermediate stages in the program. The program uses standard Java I/O classes in the overloaded method waitTillUserPressesEnter().
Listing 1-1. The InputUtil class generates a pause in Java programs
package book.util;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class InputUtil
{
public static void main(String[] args)
throws Exception
{
String line = waitTillUserHitsEnter();
System.out.println( line );
}
public static String waitTillUserHitsEnter( String message )
throws IOException
{
System.out.println( message );
return waitTillUserHitsEnter();
}
public static String waitTillUserHitsEnter()
throws IOException
{
System.out.println("Press Enter to continue..." );
BufferedReader standardInput = new BufferedReader(
new InputStreamReader( System.in ) );
String line = null;
line = standardInput.readLine();
return line;
}
}