EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

 

โœ… How to Display Text with printf in Java

Basic Syntax

System.out.printf(formatString, arguments);
  • formatString contains text with format specifiers.

  • arguments are values you want to insert into the text.


Common Format Specifiers

Specifier Meaning Example
%s String "Hello %s"
%d Integer "Number: %d"
%f Floating-point number "Price: %.2f"
%n New line (platform independent)

๐Ÿ”ข Example: Display formatted text

public class PrintfExample
{
public static void main(String[] args)
ย {
ย  String name = "Alice";
ย  int age = 30;
ย  double salary = 12345.6789;
ย  System.out.printf("Name: %s%n", name);
ย  System.out.printf("Age: %d years%n", age);
ย  System.out.printf("Salary: $%.2f%n", salary);
ย }
}

๐ŸŸข Output

Name: Alice
Age: 30 years
Salary: $12345.68

Tips:

  • %n is better than \n because it works across OSes.

  • Use %.2f to show 2 decimal places for floats/doubles.

 

 

 

other java programs

Displaying text with printf in java language

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer