This is the code I am 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 the outputs? This is what it currently looks like:
Jaden100000.0
Angela70000.0
Bob10000.0
This question is related to
java
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 );
}
}
code:
class Main
{
public static void main(String[] args)
{
int a=10, b=20;
System.out.println(a + " " + b);
}
}
Input: none
Output: 10 20
+"\n" + can be added in print command to display the code block after it in next line
E.g. System.out.println ("a" + "\n" + "b") outputs a in first line and b in second line.
Like this?
System.out.println(Name + " " + Income);
System.out.println(Name + " " + Income);
Is that what you mean? That will put a space between the name and the income?
You can use System.out.printf()
like this if you want to get nicely formatted
System.out.printf("%-20s %s\n", Name, Income);
Prints like:
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 could also add formatting to the Income argument so that the number is printed as desired
Check out this for a quick reference
Source: Stackoverflow.com