8.6. Exercises

You can download the source code for the examples in the book and the solutions to the following exercises from http://www.wrox.com.

  1. Use a StreamTokenizer object to parse a string entered from the keyboard containing a series of data items separated by commas and output each of the items on a separate line.

  2. Create a class defining an object that will parse each line of input from the keyboard that contains items separated by an arbitrary delimiter (for example, a colon, or a comma, or a forward slash, and so on) and return the items as an array of type String[]. For example, the input might be:

    1/one/2/two

    The output would be returned as an array of type String[] containing "1", "one", "2", "two".

  3. Write a program to generate 20 random values of type double between −50 and +50 and use the printf() method for System.out to display them with two decimal places in the following form:

    1) +35.93   2) −46.94   3) +42.27   4) +32.09   5) +29.21
       6) +13.87   7) −47.87   8) +30.67   9) −25.20  10) +29.67
      11) +48.62  12)  +6.70  13) +28.97  14) −41.64  15) +16.67
      16) +17.01  17)  +9.62  18) −15.21  19)  +7.46  20)  +4.09
  4. Use a Formatter object to format 20 random values of type double between −50 and +50 and output the entire set of 20 in a single call of System.out.print() or System.out.println().

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.