Skip to main content

Student Portal > Contests > 

Programming Contest Central

   
Welcome students Resources Competition ProfilesFAQ
  Contest calendar
  Problem of the month
  Past problems
  Games and brainteasers
  Successes
  Tutorials

String Tokenizer

/**
 * The StringTokenizer class breaks up a
 * String into smaller Strings. It breaks
 * up the main String at a given delimiter.
 * This program asks the user for the
 * delimiter and the String, and then outputs
 * all the String tokens.
 */
import java.io.*;
import java.util.StringTokenizer;

public class StringTokenizer {
  public static void main (String argv[]) throws IOException
  {
    String phrase;
    StringTokenizer myTokenizer;

   
    BufferedReader reader =
      new BufferedReader( new InputStreamReader( System.in ) );
   
    System.out.println("Please enter the phrase to split up.");
    phrase = reader.readLine();
    System.out.println("Name at what delimiter to split the phrase up?");

    /**
     * Instantiate myTokenizer using parameters String phrase,
     * String delimiter. This means that the StringTokenizer
     * will split up the phrase into tokens at the delimiter that the user
     * inputs.
     */
    myTokenizer = new StringTokenizer(phrase,reader.readLine());
   
    System.out.println("The tokens for your phrase are:");

    // loop through myTokenizer tokens until there are no more tokens
    while (myTokenizer.hasMoreTokens()) {
      // print out the value of current token
      System.out.println(myTokenizer.nextToken());
    } 
  }
}


Back to top
 


Quick links

Student forum

Teacher resources

RSS Feed