- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to format strings into table
To format strings into table, use the System.out.format.
For our table, we have given 15s, that means 15 spaces. In the same way for the second column after the indices.
|%1$-15s|%2$-15s|
Added the above in a string, so that we don’t have add it again and again.
The following is an example.
Example
public class Demo { public static void main(String []args) { String strFormat = "|%1$-15s|%2$-15s|
"; System.out.format(strFormat, "One", "Two"); System.out.format(strFormat, "Three", "Four"); System.out.format(strFormat, "Five", "Six"); System.out.format(strFormat, "Seven", "Eight"); System.out.format(strFormat, "Nine", "Ten"); System.out.format(strFormat, "Eleven", "Twelve"); System.out.format(strFormat, "Thirteen", "Fourteen"); System.out.format(strFormat, "Fifteen", "Sixteen"); } }
Output
|One |Two | |Three |Four | |Five |Six | |Seven |Eight | |Nine |Ten | |Eleven |Twelve | |Thirteen |Fourteen | |Fifteen |Sixteen |
- Related Articles
- Java Program to Parse and Format a Number into Binary
- Java Program to split into a number of sub-strings
- Java Program to format time using Custom Format
- Java Program to format Month in MMMM format
- Java Program to format LocalDateTime as ISO_WEEK_DATE format
- Java Program to format LocalTimeDate as BASIC_ISO_DATE format
- Java Program to Format time in AM-PM format
- Java Program to Compare Strings
- Java Program to format hour in H (0-23) format in Java
- Java Program to format a string
- How to format strings in TypeScript?
- Java Program to Compare Two Strings
- Java Program to Create random strings
- Java Program to format date in mm-dd-yyyy hh:mm:ss format
- Java Program to format date with SimpleDateFormat

Advertisements