Code example for Scanner

Methods: nextuseDelimiter

0
package com.awaj5.chapter02; 
import java.util.Scanner; 
/** 
 * 
 * @author steve 
 */ 
public class DelimiterDemo { 
     
    static public void main(String[] args) {
        Scanner keyboard1 = new Scanner(System.in);
        Scanner keyboard2 = new Scanner(System.in);
         
        keyboard2.useDelimiter("##");
        //Delimiter for keyboard1 is whitespace 
        //Delimiter for keyboard2 is ## 
         
        String word1, word2;
        System.out.println("Enter a line of text:");
        word1 = keyboard1.next();
        word2 = keyboard1.next();
        System.out.println("For keyboard1 the two words read are:");
        System.out.println(word1);
        System.out.println(word2);
        String junk = keyboard1.nextLine(); //To get rid of rest of line
         
        System.out.println("Reenter the same line of text:");
        word1 = keyboard2.next();
        word2 = keyboard2.next();
        System.out.println("For keyboard2 the two words read are:");
        System.out.println(word1);
        System.out.println(word2);
    } 
}