How to add space between two outputs?

This is the code I'm working with.

public void displayCustomerInfo() { System.out.println(Name + Income); } 

I use a separate main method with this code to call the method above:

 first.displayCustomerInfo(); second.displayCustomerInfo(); third.displayCustomerInfo(); 

Is there a way to easily add spaces between outputs? It looks like this:

 Jaden100000.0 Angela70000.0 Bob10000.0 
+9
source share
6 answers

Add a literal space or tab:

 public void displayCustomerInfo() { System.out.println(Name + " " + Income); // or a tab System.out.println(Name + "\t" + Income); } 
+18
source

You can use System.out.printf() as follows if you want a good format

 System.out.printf("%-20s %s\n", Name, Income); 

Print as:

 Jaden 100000.0 Angela 70000.0 Bob 10000.0 

This format means:

 %-20s -> this is the first argument, Name, left justified and padded to 20 spaces. %s -> this is the second argument, Income, if income is a decimal swap with %f \n -> new line character 

You can also add formatting to the Income argument so that the number is printed as desired.

Check it out for quick reference.

+13
source
 System.out.println(Name + " " + Income); 

Is that what you mean? Will this lead to a gap between name and income?

+3
source

Like this?

  System.out.println(Name + " " + Income); 
+1
source

+ "\ n" + can be added to the print command to display the code block after it in the next line

For instance. System.out.println ("a" + "\ n" + "b") displays a in the first line and b in the second line.

0
source
 import java.util.Scanner; public class class2 { public void Multipleclass(){ String x,y; Scanner sc=new Scanner(System.in); System.out.println("Enter your First name"); x=sc.next(); System.out.println("Enter your Last name"); y=sc.next(); System.out.println(x+ " " +y ); } } 
-1
source

Source: https://habr.com/ru/post/956034/


All Articles