Title: Scanner and Formatting
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Scanning and Formatting

Practice Self Test Exercises:

1. What is the difference between println and print?
2. What is the ouput produced by the following lines?


System.out.println(2 + " " + 2);
System.out.println(2+2);


3. We are now going to introduce printf(FormattingString, List of parameters).
The printf routine gives you a lot better control on how your output is formatted.
The "FormattingString" is sent to your screen as raw text until a '%' character is encountered.
The '%' character is matched up (with the exception of %n) with a parameter from the List.
We will need to do some simple examples here to demo this.
4. The following table has a list of some of the conversion options.

5. Conversion Character | Type of Output | Examples
d | Decimal (ordinary) integer | %5d %d
f | Fixed-point(everyday notation) floating point | %6.2f %f
e | E-notation floating point | %8.3e %e
g | General floating point (Java decides whether to use E-notation or not) | %8.3g %g
s | String | %12s %s
c | Character | %2c %c
n | New line | %n

6. See what happens if you slip in a "-" immediately after the % .... Note that you get "left justified" output.
7. What is the output produced by the following code?


String aString = "Jelly beans";

System.out.println("Start1234567890");
System.out.printf("Start%sEND %n", aString);
System.out.printf("Start%4sEND %n", aString);
System.out.printf("Start%13sEND %n", aString);
System.out.println();


8. Write a program that reads in a student's first name, last name, age, and GPA and print it out in some formatted fashion. Use the field widths to align the columns of your data.
9. Write a program to format a double into all of the choices above: f, e, g to see what it does.
What happens if you try the d format for a double?
10. Pick more examples from the possible choices in the table above.
11. What output is produced by the following code? For each output line, describe whether the line begins or ends with a blank or blanks.


String aString = "Jelly beans";
		double d = 123.1234567890;
		System.out.println("Start1234567890");
		System.out.printf("Start%sEND %n*** %9.4f****%n***", aString,d);


12. See the chapter 2 examples: ScannerDemo, ScannerDemo2, SelfService, PrintfDemo.java
13. Write a Java statement to output the value in variable d of type double to the screen. The output should be in e-notation with three digits after the decimal point. The output should be in a field of width 15.

Sample Programs on Video


public class Printf {

    public static void main(String[] args) {
        int i1 = 100;
        double d1 = 123.456789;
        String s="abcdefg";

        System.out.printf("xxx%20syyyzzzz",s);     // 20 space for a right justified string
        System.out.println();
        System.out.printf("xxx%-20syyyzzzz",s);     // 20 space for a left justified string
        System.out.println();

        System.out.printf("xxx%-10dyyyzzzz%n", i1);  // 10 spaces for field ... left justified

        System.out.printf("xxx%10dyyyzzzz%n", i1);  // 10 spaces for field ... right justified


        System.out.printf("xxx%fyyyzzzz%n",d1);    // Default for fixed point field
        System.out.printf("xxx%30fyyyzzzz%n",d1);  // 30 spaces for field
        System.out.printf("xxx%30.2fyyyzzzz%n", d1);  // 30 spaces with 2 points to right of decimal

        double d2 = 1234567890.123;
        System.out.printf("xxx%30fyyyzzzz%n",d2);   // Using fixed point
        System.out.printf("xxx%30eyyyzzzz",d2);   // Using exponent format

    }

}



import java.util.Scanner;

public class StudentFormatting {

    public static void main(String[] args) {
        System.out.println("Enter student name, age, gpa");
        Scanner fred = new Scanner(System.in);
        while(true)
        {
            String name = fred.next();
            int age = fred.nextInt();
            double gpa = fred.nextDouble();

            System.out.printf("%-30s%5d%8.2f", name, age, gpa);

        }

    }

}


Last Modified: July 18, 2013 3:40 PM