How to Print Multiple Variable Lines in Java

How to print multiple variable lines in Java

You can do it with 1 printf:

System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname);

How can I print this 2 Variables in the same println "System.out.println"

As printf method in java works same like C printf function so have to use format specifiers here to identify the data type.

public class Math1 {
public static void main (String args[]) {
int abdou1 = 115;
double abdou2 = 1122.176876;
System.out.println(String.format("%d %f", abdou1, abdou2));

You can use these Format Specifiers for different data types

  • %c or %C Display characters
  • %d Displays a decimal (base 10 ) integer
  • %e or %E Display a floating point number in exponential notation
  • %f Display a floating point value in decimal format
  • %s or %S Display Strings
  • %b or %B Display boolean values
  • %g (%G) float or double use %f or %e as required
  • %o int unsigned octal value
  • %p pointer address stored in pointer
  • %s array of char sequence of characters or String
  • %u int unsigned decimal
  • %x (%X) int unsigned hex value
  • %% Display a % sign

You can use whitespace characters which are

  • space ( ' ' )
  • tab ( '\t' )
  • carriage return ( '\r' )
  • newline ( '\n' )
  • ormfeed ( '\f' )

For more further explanation and examples with other data types you can go through this link.

Format Specifiers

How to print multiple integers on new lines in Java

You can do i with several ways :

This will print the variable and between each a new-line char :

System.out.println(a + "\n" + b + "\n" + c + "\n" + d);

You can also use method reference and Arrays, this will create a dynamic List from the array composed of your 4 variables, and apply System.out::println to each one :




Use a basic for each loop can also be a way :

for (int i : new int[]{a, b, c, d})

Print with format also :


How to print two variables in same line

public class hello {

public static void main(String[] args) {

String name = "daljeet";

int age = 16;

System.out.println(name + " " + age);

Java: Making System.out.println() argument span multiple lines?

Are you looking for something like this?

String line = "Information and stuff" + 
"More information and stuff " +
"Even more information and stuff";


How to format and print multiple strings in Java

You can use String.format:

System.out.println(String.format("%s %s", First, Last));

Alternatively, you can just add a space there manually:

System.out.println(First + ' ' + Last);

Java System.out.println syntax help for multiple variables

Let's say you have:

String one = "1";
String two = "2";
String three = "3";

System.out.println("one: " + stringOne + " and two: " + stringTwo + " and also a three: " + stringThree);

Will print

one: 1 and two: 2 and also a three: 3

It is called concatenation. I.e. you "create a new String".

Look at this answer too for mor information.

In you actual code " " will just add a white space between the values of your variables.

printing multiple lines with two for loop

The problem is you have your print statement inside the loop, so if the element you are viewing is positive, it is going to display "positive" on the screen. My suggestion, add a boolean variable already set to true, then if the element is negative, change it to false inside your loop, then return that variable. Use that to display your positive string outside of the loop. Hope this helps!

Related Topics

Leave a reply