AddUp Program

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:

The blanks are filled in the following.

AddUp Program

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

public class AddUp
{

  public static void main ( String[] args ) throws IOException
  {
    StringTokenizer tok;
    int sum = 0;

    BufferedReader inData = new BufferedReader 
        ( new InputStreamReader( System.in ) );

    System.out.println("Please enter the data:" );
    String inString = inData.readLine();
    tok = new StringTokenizer( inString );

    while ( tok.hasMoreTokens() )
    {
      String intSt = tok.nextToken() ;
      int data     = Integer.parseInt( intSt );
      sum         += data;
    }

    System.out.println("Sum is: " + sum );
  }
}

This program exects all the input to appear on one line. If input is expected on several lines, build a StringTokenizer for each line.

QUESTION 21:

Does the StringTokenizer for each line have to use the same delimiters?