Best 2 Ways to Give Tab Space in Java

In this tutorial, we will learn how to give tab space in Java. Here we have used the “\t” tab space with the example of both numbers and Strings.

What is tab space in Java?

Tab space is the combination of the 8 characters at a time. So we can also say that it is the combination of the next 8 spaces at a time approximately.

Most of the programmers are used this tab space for the indentation of coding to make it more readable.

How to give a tab space in Java? (Logic Behind Me)

We can use a tab space in Java by using a simple “\t” symbol along with your code. We can place one “\t” in-between two strings to represent it with the tab space. We can use this tab space with both the numbers and as well as with the Stings also.

Example-1: System.out.print(“Happy\tCoding”);

Output-1: Happy Coding

Example-2: System.out.print(45 + “\t” + 56);

Output-2: 45 56

Example-3:

String str1 = “Coding”, str2 = “Face”;

System.out.print(str1 + “\t” + str2);

Output-3: Coding Face

Also Read: Palindrome In Java(String & Number) – Check 2 Easy Methods

Program-1: How to Print Two Numbers With Tab Space in Java?

We can print the two numbers with tab space in Java by using a simple “\t” symbol between two numbers. We can also put multiple tabs at a time. Let’s see the complete program now.

/**
 * Java program to print two numbers with tab space in Java.
 */
package cf.java.logical;

import java.util.Scanner;

public class PrintTwoNumbersWithTabSpace {

	public static void main(String[] args) {
		// Declare all the variables
		int num1, num2;
		// Accept any two numbers from user
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter any two numbers: ");
		num1 = sc.nextInt();
		num2 = sc.nextInt();
		// Display both the numbers with tab space
		System.out.println("Given two numbers with one tab space: " + num1 + "\t" + num2);
		System.out.println("Given two numbers with two tab space: " + num1 + "\t\t" + num2);
		System.out.println("Given two numbers with three tab space: " + num1 + "\t\t\t" + num2);
	}

}Code language: PHP (php)

Output:

Enter any two numbers: 16 35
Given two numbers with one tab space: 16	35
Given two numbers with two tab space: 16		35
Given two numbers with three tab space: 16			35Code language: JavaScript (javascript)

Program-2: How to Print Two Strings With Tab Space in Java ?

We can print the two Strings with tab space in Java by using a simple “\t” symbol between two Strings. We can also put multiple tabs at a time. Let’s see the complete program now.

/**
 * Java program to print two Strings with tab space in Java.
 */
package cf.java.logical;

import java.util.Scanner;

public class PrintTwoStringsWithTabSpace {

	public static void main(String[] args) {
		// Declare all the variables
		String str1, str2;
		// Accept any two numbers from user
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter any two Strings: ");
		str1 = sc.nextLine();
		str2 = sc.nextLine();
		// Display both the numbers with tab space
		System.out.println("Given two Strings with one tab space: " + str1 + "\t" + str2);
		System.out.println("Given two Strings with two tab space: " + str1 + "\t\t" + str2);
		System.out.println("Given two Strings with three tab space: " + str1 + "\t\t\t" + str2);
	}

}
Code language: JavaScript (javascript)

Output:

Enter any two Strings: Tab
Space
Given two Strings with one tab space: Tab	Space
Given two Strings with two tab space: Tab		Space
Given two Strings with three tab space: Tab			SpaceCode language: JavaScript (javascript)

Also Read: How To Find Multiples Of A Float Value In Java?

Note:

In both of the above programs, we have used the “\t” symbol with a concatenation operator. We can also use “\t” tab space within one whole String also. Inside of one whole string, we can place the “\t” tab space and when it will be displayed, it will be displayed with the tab space automatically.

Example:

System.out.println(“Coding\tface”);

Output:

Coding face

Other Queries We Have Also Covered Here

  • How to print tab space in java?
  • How to leave tab space in java?
  • How to give a tab space in java?
  • How many spaces is a tab in Java
  • How to gice tab space in Java output?
  • How to provide tab space in Java?

FAQs

How many spaces are in a tab in Java?

One tab is mostly 8 characters in length. So we can also say that it is the combination of the next 8 spaces at a time approximately

Is Tab is equal to 2 spaces?

No, it’s approximately equal to 8 spaces.

Can we use Tab directly in our program?

Yes, we can use \t for tab space in our program. It will automatically arrange the numbers or Strings with proper indentation.

Why we should use Tab in Java?

Most of the programmers are used this tab space for the indentation of coding to make it more readable.

2 thoughts on “Best 2 Ways to Give Tab Space in Java”

Leave a Comment