Sunday, April 2, 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 21, 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

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

The java.lang bundle has no built-in desk turbines, however you possibly can simply current knowledge in a chart-based format should you creatively use Java’s printf technique with dashes, pipelines and printf area specifiers.

A easy, Java printf desk displaying primitive sort particulars.

To reveal how one can 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 achieved. Let’s construct towards that because the purpose.

Java printf desk specs

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

  • Dashed traces act as row separators.
  • Pipelines separate columns.
  • Every cell have a single house 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 will not 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", "quick",   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 varieties, together with attention-grabbing particulars about every of them.

 



Source link

You might also like

So why did they decide to call it Java? – InfoWorld

Senior Java Developer – IT-Online

West Java to provide simultaneous polio vaccinations from Apr 3 – ANTARA English

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

So why did they decide to call it Java? – InfoWorld

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

So why did they decide to call it Java?  InfoWorld Source link

Read more

Senior Java Developer – IT-Online

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

Senior Java Developer  IT-On-line Source link

Read more

West Java to provide simultaneous polio vaccinations from Apr 3 – ANTARA English

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

West Java to provide simultaneous polio vaccinations from Apr 3  ANTARA English Source link

Read more

COBOL programming skills gap thwarts modernization to Java – TechTarget

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

COBOL programming skills gap thwarts modernization to Java  TechTarget Source link

Read more

User input with a Java JOptionPane example – TheServerSide.com

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

User input with a Java JOptionPane example  TheServerSide.com Source link

Read more
Next Post
Review: Visual Studio Code shines for Java

Review: Visual Studio Code shines for Java

Leave a Reply Cancel reply

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

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

15+ Top C++ Online Courses & Certificates in 2023 (Free and Paid) – Fordham Ram

February 20, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Quick Tip: How to Use the Spread Operator in JavaScript — SitePoint – SitePoint

February 28, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Top 10 C++ Developer Jobs to Apply for in Big Tech Companies – Analytics Insight

February 15, 2023

Browse by Category

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

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – 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?