Tuesday 5 March 2019

How to format the string using java.util.Formatter class


Click here to watch on Youtube:
https://www.youtube.com/watch?v=qt89kDYMBGo&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge:

How to format the string using java.util.Formatter class

FormatterDemo.java

import java.util.Formatter;

/**
 * public Formatter format(String format, Object... args)
 *
 * Parameters:
 *
 * format - A format string as described in Format string syntax.
 *
 * args - Arguments referenced by the format specifiers in the format
 * string. If there are more arguments than format specifiers, the
 * extra arguments are ignored. The maximum number of arguments is
 * limited by the maximum dimension of a Java array as defined by The
 * Java™ Virtual Machine Specification.
 *
 * Returns: This formatter
 *
 */

public class FormatterDemo
{

    public static void main(String[] args)
    {
        Formatter formatter = new Formatter();

        /*
         * In this example, the format specifiers, %s and %d are
         * replaced with the arguments that follow the format string.
         * %s is replaced by "Peter", %d is replaced by 10. %s
         * specifies a string, and %d specifies an integer value. All
         * other characters are simply used as-is.
         */

        formatter.format("%s age is %d", "Peter", 10);
        System.out.println(formatter.out());
        formatter.close();
    }

}

Output:

Peter age is 10

Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Formatter.html

Click the below link to download the code:
https://sites.google.com/site/javaspringram2019/java_spring_2019/FormatterDemo_Intro.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java_Spring_2019/tree/master/Java_2019/FormatterDemo_Intro

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/ef6f73b2185c28cb9cd0149bcd1e9fa0a5b2bdf3/Java_2019/FormatterDemo_Intro/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment