JMU
Lab: Gaining Experience Formatting Strings and Output


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Creating Simple Formatted Strings: The static format() method in the String java.lang.String class can be used to create formatted String objects. It must be passed a format string and 0 or more values/objects to be formatted. In its simplest form, the format string contains a single format specifier with the following syntax:

%conversion

where the conversion can be, among other things: b for a boolean, s for a String, d for an integer (base 10), and f for a floating point number.

  1. Compile and execute FormattingOutputDriver.
  2. What output is generated?
  3. Change the conversion from d to f in the second call to format().
  4. Compile and execute FormattingOutputDriver.
  5. What exception is thrown?
  6. Change the conversion from f back to d in the second call to format().
2. Creating Formatted Strings of a Particular Width: The format specifier can also include a width specifier. That is, for our current purposes, it has the following syntax:

%[width][.precision]conversion

where the width indicates the minimal field width and the precision indicates the number of digits after the decimal place for floating point numbers and the maximum number of characters for String objects.

  1. Modify each of the format specifiers in FormattingOutputDriver so that all of the values are printed in a field of width 10.
  2. Compile and execute FormattingOutputDriver.
  3. What output is generated?
  4. Modify the format specifier in the third call to format() so that the resulting String has two digits to the right of the decimal point.
  5. Compile and execute FormattingOutputDriver.
  6. What output is generated?
  7. Modify the format specifier in the first call to format() so that the resulting String has at most two characters.
  8. Compile and execute FormattingOutputDriver.
  9. What output is generated?
3. Controlling Other Aspects of the Formatting: The format specifier can also include flags that control the formatting. That is, it really has the following syntax:

%[flag]...[width][.precision]conversion

For example, a - causes the resulting String to be left-justified, a + causes the resulting String to include a sign (for numerical values), and a , will include grouping separators (for numeric values).

  1. What String would be returned by String.format("%-10.2s", "CS239")? (Hint: Add the approrpiate code to FormattingOutputDriver and see.)
  2. What String would be returned by String.format("%,5.2f", 12345.67)?
  3. What String would be returned by String.format("%,20.2f", 12345.67)?
  4. What String would be returned by String.format("%-,20.2f", 12345.67)?
4. Creating Strings with Complicated Formatting: The format() method can be passed multiple values/objects after the format string. To accomodate this, the format string can contain multiple format specifiers. It can also contain character literals.
  1. What String would be returned by String.format("Sections: %d and %d", 1, 2)?
  2. What String would be returned by String.format("CS%3.3s", 239)?
  3. What String would be returned by String.format("2008: $%,10.2f\n2009: $%,10.2f", 92315.50, 105000.10);?
5. Formatting Output: One does not always need to store a formatted String before "printing" it. In such cases, it is often convenient to use the printf() method in the PrintStream java.io.PrintStream class. Like the format() method, the printf() method is passed a format string and 0 or more values/objects to print.
  1. What output would be generated by the following? (Be careful!)
        System.out.printf("Sections: %d and %d", 1, 2);
        System.out.printf("%,20.2f", 12345.67);
        
  2. What output would be generated by the following?
        System.out.printf("Sections: %d and %d\n", 1, 2);
        System.out.printf("%,20.2f", 12345.67);
        
6. More on Formatting: Both the format() method in the String class and the printf() method int the PrintStream class actually use a Formatter java.util.Formatter object to do the work. Hence, the documentation for this class contains all of the details of format strings and specifiers.
  1. What conversion(s) can be used to format an integer in hexadecimal (i.e., base 16)?
  2. What conversion(s) can be used to format an integer in octal (i.e., base 8)?
  3. What conversion(s) can be used to format a number in computerized scientific notation?
  4. What flag will cause zero-padding?
  5. What flag will cause negative numbers to be enclosed in parentheses?
  6. What should you do if you need the String/output to include a percent sign?

Copyright 2011