Source of DelimitersDemo.java


  1: //DelimiterDemo.java
  2: //Illustrates how to set the delimiter for reading strings.
  3: 
  4: import java.util.Scanner;
  5: 
  6: public class DelimitersDemo
  7: {
  8:     public static void main(String[] args)
  9:     {
 10:         Scanner keyboard1 = new Scanner(System.in);
 11:         Scanner keyboard2 = new Scanner(System.in);
 12: 
 13:         keyboard2.useDelimiter("##");
 14:         //The delimiters for keyboard1 are the white space characters.
 15:         //The only delimiter for keyboard2 is ##.
 16: 
 17:         String s1, s2;
 18: 
 19:         System.out.println("Enter a line of text with two words:");
 20:         s1 = keyboard1.next();
 21:         s2 = keyboard1.next();
 22:         System.out.println("The two words are \"" + s1 +
 23:             "\" and \"" + s2 + "\"");
 24: 
 25:         System.out.println("Enter a line of text with two words");
 26:         System.out.println("delimited by ##:");
 27:         s1 = keyboard2.next();
 28:         s2 = keyboard2.next();
 29:         System.out.println("The two words are \"" + s1 +
 30:             "\" and \"" + s2 + "\"");
 31:     }
 32: }