Saturday, March 25, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

How to format a Java table with printf example

learningcode_x1mckf by learningcode_x1mckf
November 9, 2022
in Java
0
How to format a Java table with printf example
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


Print and format a desk with printf

Typically it’s good to format the output of a console based Java program in a pleasant method.

The java.lang package deal has no built-in desk mills, however you possibly can simply current information in a chart-based format in case you creatively use Java’s printf methodology with dashes, pipelines and printf discipline specifiers.

A easy, Java printf desk displaying primitive sort particulars.

To reveal format a desk with Java printf statements, let’s create a chart that shows details about Java’s 8 primitive types.

Above is how the completed printf desk will look once we are completed. Let’s construct towards that because the objective.

Java printf desk specs

We’ll format the Java printf desk in accordance with the next guidelines:

  • Dashed traces act as row separators.
  • Pipelines separate columns.
  • Every cell have a single area of padding on the left and proper.
  • The primary column is 10 areas huge, plus padding.
  • The second column is 8 areas huge, plus padding.
  • The third column is 4 areas huge, plus padding.
  • Integer values within the third column are zero padded to 4 digits.

Create the printf desk title

We will simply hardcode the title:

System.out.printf("--------------------------------%n");
System.out.printf("    Java's Primitive Varieties      %n");
System.out.printf("    (printf desk instance)      %n");
System.out.printf("--------------------------------%n");

The one printf placeholder used within the first 4 traces is %n, which merely causes a carriage return that places subsequent output on a brand new line.

Java Printf Table chart

This picture reveals the values used to create our desk with Java printf statements.

Code the printf desk’s heading

The center of the printf desk will use the next three placeholders:

  • %-10s to allocate 10 spaces for text String display. The minus indicators forces left justification.
  • %-8s to allocate 8 areas for textual content String show. Once more, the minus indicators forces left justification.
  • %04d, to allocate 4 areas to show the digits. The 0 in %04d causes zero fills when a quantity shouldn’t be 4 digits lengthy.

With the added pipeline characters and padding, the Java printf line to format the desk heading appears to be like as follows:

System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS");

Java printf desk physique instance

The next is the entire code for the Java printf desk instance’s physique:

System.out.printf("--------------------------------%n");
System.out.printf(" Java's Primitive Varieties         %n");
System.out.printf(" (printf desk instance)         %n");

System.out.printf("--------------------------------%n");
System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS");
System.out.printf("--------------------------------%n");

System.out.printf("| %-10s | %-8s | %04d |%n", "Floating", "double",  64);
System.out.printf("| %-10s | %-8s | %04d |%n", "Floating", "float",   32);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "lengthy",    64);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "int",     32);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "char",    16);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "brief",   16);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "byte",    8);
System.out.printf("| %-10s | %-8s | %04d |%n", "Boolean",  "boolean", 1);

System.out.printf("--------------------------------%n");

When the code runs, a good-looking Java printf desk shows, displaying a chart of the eight Java primitive sorts, together with attention-grabbing particulars about every of them.

 



Source link

You might also like

Developing Multi-Threaded Applications with Java Concurrency API – Data Science Central

Gems & Java helps Mothers with a Heart for Ethiopia change lives – Woodstock Sentinel Review

New java joint Kiki's Coffee opening near downtown San Antonio's Maverick Park – San Antonio Current

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Developing Multi-Threaded Applications with Java Concurrency API – Data Science Central

by learningcode_x1mckf
March 25, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Developing Multi-Threaded Applications with Java Concurrency API  Information Science Central Source link

Read more

Gems & Java helps Mothers with a Heart for Ethiopia change lives – Woodstock Sentinel Review

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Gems & Java helps Mothers with a Heart for Ethiopia change lives  Woodstock Sentinel Evaluation Source link

Read more

New java joint Kiki's Coffee opening near downtown San Antonio's Maverick Park – San Antonio Current

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

New java joint Kiki's Coffee opening near downtown San Antonio's Maverick Park  San Antonio Present Source link

Read more

Oracle's new Java 20 programming language & development … – Daijiworld.com

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle's new Java 20 programming language & development ...  Daijiworld.com Source link

Read more

Java Developer Survey Reveals Increased Need for Java … – PR Newswire

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Developer Survey Reveals Increased Need for Java ...  PR Newswire Source link

Read more
Next Post
Jamstack Panel: Multiple JavaScript Frameworks Are a Good Thing

Jamstack Panel: Multiple JavaScript Frameworks Are a Good Thing

Leave a Reply Cancel reply

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

Related News

Junior Java Developer at Reverside

Junior Java Developer at Reverside

November 12, 2022
Stop worrying about code signing!

Stop worrying about code signing!

September 29, 2022
Natural Language Processing and How ML Models Understand Text – The Real Python Podcast

Natural Language Processing and How ML Models Understand Text – The Real Python Podcast

September 7, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Developing Multi-Threaded Applications with Java Concurrency API – Data Science Central
  • Gems & Java helps Mothers with a Heart for Ethiopia change lives – Woodstock Sentinel Review
  • 4 Ways to Remove a Specific Item From a JavaScript Array – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?