go to previous page   go to home page   go to next page

Answer:

Pattern quitPattern = 
  Pattern.compile(  "q(uit)?" , Pattern.CASE_INSENSITIVE ) ;

User-Friendly

Here is a skeleton of a program that asks the user for a series of values which are then processed in some way. Instead of entering a value, the user may enter quit to signal the end of the values.

import java.util.regex.*;
import java.util.*;

public class Continue
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner( System.in );    
    String response ;

    // Pattern for leaving the program  
    Pattern exitPat = Pattern.compile( "q(uit)?", Pattern.CASE_INSENSITIVE );    
   
    System.out.print("Enter first value, or quit: ");
    response = scan.nextLine();

    while ( !exitPat.matcher( response ).matches() )
    {
      System.out.println("Processing: " + response );      
      // Do processing Here  
      
      System.out.print("Enter next value, or quit: ");
      response = scan.nextLine();
    }
    
    System.out.println("Done Processing.");      
  }
}


QUESTION 9:

Create a Pattern that matches a three-digit number and also saves each digit in a capture group.

Pattern quitPattern = 
  Pattern.compile( ) ;