import java.io.File;
import java.io.PrintWriter;


// Introduction to Java I/O

public class JavaOutput {

    public static void main(String[] args) throws Exception {
        // Java output is pretty straight forward
        // e.g. if we want to print the following:
        int a = 1;
        char b = 'Z';
        String c = "ho ho ho!";
        
        // do this:
        System.out.println("" + a + b + c);
        
        // or you can do it one by one
        System.out.print(a);
        System.out.print(b);
        System.out.println(c);
        
        
        // To print to a file use:
        PrintWriter fout = new PrintWriter(new File("outputFile"));
        
        // e.g. print the contents of a integer array seperated by spaces, in base 3
        int[] myArray = new int[]{1, 4, 2, 3, 4, 2};
        for(int i = 0; i < myArray.length; i++) {
            if (i != 0) System.out.print(' ');
            System.out.print(Integer.toString(myArray[i], 3));
        }
        System.out.println();
        
        
        /*
         * Printing a floating point number is more work
         * Usually there is a certain format (e.g. number of decimal places)
         * Thanks to Java 1.5, we now have format (similar to C++ printf)
         */
        
        // suppose we want to print the following integers and doubles
        int[] ints = new int[]{3, 2, 15};
        double[] doubles = new double[]{2.2, 0.43, 25.267};
        
        /*
         * into
         * " 3  2.20"
         * " 2  0.43"
         * "15 25.27",
         * 
         * do:
         */
        
        for(int i = 0; i < 3; i++) {
            System.out.format("%2d %5.2f\n", ints[i], doubles[i]);
        }
        
        // There's a bunch of other options.
        // - means left justify
        // 0 means leading zeros
        //%s is for strings
        
        for(int i = 0; i < 3; i++) {
            System.out.format("%02d %-5.2f\n", ints[i], doubles[i]);
        }
        
        /*
         * Gives:
         * "03 2.20 "
         * "02 0.43 "
         * "15 25.27"
         */
        
        // Finally, in the spirit of variable arguments
        String[][] words= new String[][]{
	    {"First", "Last", "Fav Food", "e-mail"},
	    {"Winnie", "Pooh", "Honey", "pooh@100acrewoods.net"}
        };
        for(String[] sa: words)
            System.out.format("%4$-25s %1$-10s %2$-10s\n", sa);
        /*
         * gives:
         * "e-mail                    First      Last      "
         * "pooh@100acrewoods.net     Winnie     Pooh      "
         */   
    }

}
