Monday July 10, 2000


Consider the following exercise.

Exercise

Niels Henrik wishes to write a program to do the following.

1. Prompt the user to input the following information.

This information will be stored in a string called input and delimited with spaces.

2. Based on the number of television sets entered, the user is prompted to enter the cost of each set.

These costs are stored in a string called costs and delimited with spaces.

3. The string called costs is now broken apart into individual costs and the sum, average, maximum, and minimum costs of the sets are computed.
4. Each of these is converted into a string and concatenated onto the string input.

5. Then the string input is broken apart and each piece of information printed on a seperate line.
In the String class there is a method called valueOf() that is helpful. It allows to convert certain data types into strings.

This method is overloaded.

Some of the signatures of this method are:

  • public static String valueOf(int i)

  • public static String valueOf(float f)

  • public static String valueOf(double d)

  • public static String valueOf(char c)

  • public static String valueOf(boolean b)

    So these methods give us a way of converting some primitive data types to strings.

    Recall some of the methods that are available to us in the StringTokenizer class.
    public class StringTokenizer implements Enumeration {
       public StringTokenizer(String str,String delim,boolean returnTokens);
       public StringTokenizer(String str,String delim);
       public StringTokenizer(String str);
       public boolean hasMoreTokens();
       public String nextToken();
       public String nextToken(String delim);
       public boolean hasMoreElements();
       public Object nextElement();
       public int countTokens();
    }
    

    A Solution