User-specified Delimiters

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.

A good answer might be:

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

User-specified Delimiters

Here is the program again, but this time with the delimiters specified in the constructor. The delimiters are now space, equals, plus, and minus.

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, " =+-" ); // note the space before =

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

QUESTION 17:

What is the output for the following:

C:/> java TokenTester
Enter a string: val = 12+8