Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

CHAPTER 1: Performance Toolkit > A Utility to Pause in a Java Program

A Utility to Pause in a Java Program

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;
  }
}

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial