More than one Delimiter Works

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
12  8  5  32

A good answer might be:

4 tokens, delimited by spaces.

More than one Delimiter Works

Tokens can be separated by more than one delimiter. There are several spaces separating the tokens in the example. Here are some methods of the class:

StringTokenizer Methods int countTokens() return the number of tokens remaining. boolean hasMoreTokens() return true if there are more tokens. String nextToken() return the next token.

The countTokens() method starts with the total number of tokens the String has been broken into. Each time nextToken() is called, one token (starting with the first) is consumed, and countTokens() will report one less token. Here is an example program:

import java.io.*;
import java.util.*;

public class TokenTester
{
  public static void main ( String[] args ) throws IOException
  {
    BufferedReader stdin = 
      new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Enter a string:");
    String data = stdin.readLine();   
    
    StringTokenizer tok = new StringTokenizer( data );

    while ( tok.hasMoreTokens() )
      System.out.println( tok.nextToken() );
  }
}

QUESTION 16:

What will the output be for the following:

C:/> java TokenTester
Enter a string: 12  8  5  32