formatting

4 0 0
                                    

Streams - this is a river of data where you get an input or output. Formatted Stream object is used to covert stream data to formatted output. Any object that implements formatted stream output is an instance of either PrintWriter or PrintStream.

PrintStream - this is byte stream class that converts stream data to formatted output. System.out and System.err are examples of a PrintStream object.

          Why? 

          System is a class that cannot be instantiated. It is static. You use the System name class itself to call out function or properties. System class is the class that provides standard input, output and error. 

          out is a PrintStream object that was defined probably within the System class. It was created as a property within that System class. Possible like the program below.

class System{

    PrintStream out

    public System(){

        out = new PrintStream();

    }

    // list of functions accessible to out

}

         This PrintStream out object can access functions define like println, print and format. Since this is a byte stream, it needs to convert byte to character. So, it uses either Console.charset() or Charset.defaultCharset().

PrintWriter - this is a character stream class that converts stream data to formatted output. Based on the previous stream examples, it is convenient to use character streams since you can omit the hassle of converting byte to character. Based on this tutorial, PrintWriter and PrintStream defined methods with the same functions like println or format.

==================

class PracticingPrintWriter{

    public static void main(String[] args) throws  IOException{

        String yourFileName = "JustATemporary.txt";

        writeSomething(yourFileName);

        System.out.println(readSomething(yourFileName));

    }

    private static void writeSomething(String fileName) throws IOException{

        try(PrintWriter writer = new PrintWriter(fileName);

               Scanner scanner = new Scanner(System.in)

        ){

            System.out.println("Enter something below:");

            String gatherItHere = "";

            while(scanner.hasNext()){

                String message = scanner.next();

                String addingSpace = message.concat(" ");

                gatherItHere = gatherItHere.concat(addingSpace);

                if(message.equals("999")){

                    break;

                }

            }

            writer.write(gatherItHere);

        }

    }

    private static String readSomething(String fileName) throws IOException{

        try(FileReader reader = new FileReader(fileName);

               Scanner scanner  = new Scanner(reader)

        ){

            String gatherItHere = "";

            while(scanner.hasNext()){

                String message = scanner.next();

                String addingSpace = message.concat(" ");

                gatherItHere = gatherItHere.concat(addingSpace);

            }

            return gatherItHere;

        }

    }

}

==================

Result:

Hello Java.

I am practicing programming.

999

Hello Java. I am practicing programming. 999


Note: If you notice on my sample programs, I don't use import since I am using Intellij as an IDE. It supply the imports automatically.


format() - It works like println but you do more in here than in println.

syntax:

    double num1 = 55;

    System.out.format("%f, %1$+020.10f %n", num1);

    % - format specifier

    f - float. You want to convert num1 to float.

    1$ - argument index. Meaning you want to format the first argument again. < means previous argument.

    +0 - sign and padding character. You can use -1 for a negative.

    20 - width of the num1. The default for padding is blank.

    .10 - precision. The number of decimal points to specify.

    num1 - This is the number you want to format.


====================

class PracticingFormat{

    public static void main(String[] args){

        double num1 = 50;

        double num2 = -55;

        System.out.format("%f, %1$+020.3f %n", num1);

        System.out.format("%f, %<+20.3f %n", num1);

        System.out.format("%f, %f, %2$-120.10f %n", num1, num2);

    }

}

====================

Result:

50.000000, +000000000000050.000

50.000000,                                  +50.000

50.000000, -55.000000, -55.0000000000

Java ProgrammingWhere stories live. Discover now