javacodex.com
Java Examples
Java Examples
memu home questions

Scanner and String Tokenizer Example

This jave example demonstrates how to take an input from a scanner and split it using a string tokenizer.

The Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.

The StringTokenizer class allows a program to break a string into tokens.

Source: (Example.java)

import java.util.*;
 
public class Example {
   public static void main(String[] args) {
 
      System.out.print("Enter a string: ");
 
      Scanner sc = new Scanner(System.in);
      String str = sc.nextLine();
 
      StringTokenizer st = new StringTokenizer(str);
      while (st.hasMoreElements()) {
         System.out.println(st.nextElement());
      }
 
      sc.close();
   }
} 
 

Output:

$ java Example 
Enter a string: Welcome to Disney World 
Welcome
to
Disney
World

Java Docs:

Questions answered by this page:

Stringtokenizer example program in java Java Scanner and StringTokenizer example Reading input from a scanner. Breaking a string into tokens

Related Examples:


Contact: javacodex@yahoo.com