Home » Java programming language

Java Console format() Method with Example

Console Class format() method: Here, we are going to learn about the format() method of Console Class with its syntax and example.
Submitted by Preeti Jain, on March 31, 2020

Console Class format() method

  • format() method is available in java.io package.
  • format() method is used to write the formatted string to this Console with the help of the given string format and object parameters.
  • format() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • format() method may throw an exception at the time of formatting this Console.
    IllegalFormatException: This exception may throw when a given string format is not valid.

Syntax:

    public Console format(String frmt, Object... params);

Parameter(s):

  • String frmt – represents the format string as defined in the Format string.
  • Object... params – represents the parameters addressed by the format specifiers in the format string.

Return value:

The return type of the method is Console, it returns this Console.

Example:

// Java program to demonstrate the example 
// of Console format(String frmt , Object… params)
// method of Console

import java.io.*;

public class FormatOfConsole {
 public static void main(String[] args) {
  try {
   // Instantiates Console 
   Console con = System.console();

   // Define a string format for rows and columns
   String frmt = "%1$15s %2$20s %3$20s%n";

   // By using format() method isto format the
   // the given string in rows and column
   con.format(frmt, "Student_Name", "Student_Add", "Student_ph");
   con.format(frmt, "Preeti", "Delhi", "9425667852");
   con.format(frmt, "Rahul", "Delhi", "8871459682");
   con.format(frmt, "Shreya", "Delhi", "7216589745");
   con.format(frmt, "Rama", "Bangalore", "8871568492");
   con.format(frmt, "Anjali", "London", "8872589261");

  } catch (Exception ex) {
   System.out.println(ex.toString());
  }
 }
}

Output

Student_Name          Student_Add           Student_ph
         Preeti                Delhi           9425667852
          Rahul                Delhi           8871459682
         Shreya                Delhi           7216589745
           Rama            Bangalore           8871568492
         Anjali               London           8872589261






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.