StringTokenizer object used to tokenize strings - Java Language Basics

Java examples for Language Basics:StringTokenizer

Description

StringTokenizer object used to tokenize strings

Demo Code

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main 
{
   // execute application
   public static void main(String[] args)
   {//from   w  w  w.  j  a  v  a  2s.co  m
      // get sentence
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter a sentence and press Enter");
      String sentence = scanner.nextLine();

      // process user sentence
      String[] tokens = sentence.split(" ");
      System.out.printf("Number of elements: %d\nThe tokens are:\n",
         tokens.length);

      for (String token : tokens)
         System.out.println(token);
   } 
}

Result


Related Tutorials