Sunday, August 7, 2022

Java Program 1 - Print the details of a person in Tabular form

Program to write the details in tabular form. The tabular form means, the column names will be teh properties like, Name, Institute and Location.

TestCase:
Name: MyName
Institute: MyCollege
Location: MyLocation

Output:


  

This can be done in two ways, using System.out.print() and System.out.format().

Using System.out.print()

In this approach, first we hardcode the column names, "Name", "Institute", "Location" and then we start the for loop, this for loop will be running till the end of the largest string. Inside the for loop, we write the condition to take an empty space when the characters in any of the string is over(This is because the length of the strings could be different for different words).

As the print() function does not have any formatting options, we have to give the tab spaces based on the length of the column heading.
For example, the 2nd Column(Institute) has 9 characters and to make sure that all the rows align with the column names we will be giving the two tab spaces inside the for loop. 

Code:

public class MyDetails {
public static void main(String[] args) {
String name = "MyName";
String institute = "MyCollege";
String location = "MyLocation";

int length = Math.max(location.length(), Math.max(name.length(), institute.length()));
System.out.println("---------------------------------");
System.out.print("Name" + "\t");
System.out.print("Institute" + "\t");
System.out.print("Location");
System.out.println();
System.out.println("---------------------------------");
for(int i=0; i< length; i++) {
char nameChar = i < name.length() ? name.charAt(i) : ' ';
char instituteChar = i < institute.length() ? institute.charAt(i) : ' ';
char locationChar = i < location.length() ? location.charAt(i) : ' ';
System.out.print(nameChar + "\t");
System.out.print(instituteChar + "\t" + "\t");
System.out.println(locationChar);
}
}
}




Using System.out.format()

Similar to the above approach, here too we will be hardcoding the values of the column headings(Name, Institute and Location). But the advantage with the format() method is that this method provides the ways to format the text in a tabular form. The %10s means 10 spaces will be left before this character/String is printed. System.out.format("%s %14s %17s", "Name", "Institute", "Location"); means we leave 0 spaces before Name, 14 spaces before Institute and 17 spaces before Location. Similarly, we add the formatting in the for loop as well, and it makes sure to leave the given spaces before printing the character

public class MyDetails2 {
public static void main(String[] args) {
String name = "MyName";
String institute = "MyCollege";
String location = "MyLocation";

int length = Math.max(location.length(), Math.max(name.length(), institute.length()));
System.out.println("---------------------------------");
System.out.format("%s %14s %17s", "Name", "Institute", "Location");
System.out.println();
System.out.println("---------------------------------");
for(int i=0; i< length; i++) {
char nameChar = i < name.length() ? name.charAt(i) : ' ';
char instituteChar = i < institute.length() ? institute.charAt(i) : ' ';
char locationChar = i < location.length() ? location.charAt(i) : ' ';
System.out.format("%s %9s %18s", nameChar, instituteChar, locationChar);
System.out.println();
}
}
}


No comments:

Post a Comment

Setting up the environment variables for Java in Windows

How to set the Environment Variables for Java in Windows? Step 1 -   Go to Control Panel -> System and Security  System .  In System, sel...