• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Java String format() method with Examples

Overview

In this post, we will learn about Java String format() method with examples. String format() method is used to format strings in specified formats. It is the common way of formatting strings in Java language.

String format() Syntax

The method is a static overloaded method that is defined in the String class. The method might throw an IllegalFormatException if the specified format string is of illegal syntax.

public static String format(Locale l, String format, Object… args)

Basic Example

Format string and double value:

try {
 String name= "Mark John";
 double cost = 34.99;
 System.out.println("Bill Amount Slip");
 System.out.println(String.format("Name: %30s", name));
 System.out.println(String.format("Total Cost $:
 %20.4f", cost));
 }catch(Exception e) {
 System.out.println("Error"+ e.getMessage());
 }

For strings we have used %s format specifier. The number indicates the width of the string.
For floating point numbers we have used %f format specifier.
%20.4f prints the four numbers after the decimal point.

Display Formatted Table Example

The following Java program uses the String format() method to display the database table on to the console output window in Eclipse IDE.

https://www.testingdocs.com/display-data-from-oracle-database-table-using-jdbc/

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Java Performance

Java Tutorials /

Improving Java Performance with Multithreading

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

‹ Autoboxing and Unboxing in Java› Java Programming Language Features

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com