How to print multiple integers on new lines in Java

13k views Asked by At

I am figuring how to print the results of 4 integers on separate lines by typing only one System.out.println (I guess typing less is better for coders).

Expected Output :

43 
1 
19 
13

My code:

class JavaApplication1 {
     public static void main(String[] args) {
         int a,b,c,d;
         a = -5 + 8 * 6;
         b = (55+9) % 9;
         c = 20 + -3*5 / 8;
         d = 5 + 15 / 3 * 2 - 8 % 3 ;

         System.out.println(a);
         System.out.println(b);
         System.out.println(c);
         System.out.println(d);
    }  
}
9

There are 9 answers

0
azro On BEST ANSWER

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 :

Arrays.asList(a,b,c,d).forEach(System.out::println);

or

IntStream.of(a,b,c,d).forEach(System.out::println);

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

for (int i : new int[]{a, b, c, d})
     System.out.println(i);

Print with format also :

System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)
0
Tom Haynes On

You can use '\n' to create a new line in Java, so your code would look like this:

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

This will print them on new lines.

0
Zabuzard On

print the results of the 4 integers on separate lines by typing only once 'system.out.println'.

Quite easy, you need to add a new-line character to the text you want to print. On most machines this is represented by the special character \n.

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

However there are platform independent ways like what the method System.lineSeparator() (documentation) returns. Like:

String lineSeparator = System.lineSeparator();
String output = a + lineSeparator
    + b + lineSeparator
    + c + lineSeparator
    + d;
System.out.println(output);

Alternatively you could use the System.out.printf method (documentation), which stands for "print formatted". It has the parameter %n that creates a new line, also using System.lineSeparator() internally.

System.out.printf("%d%n%d%n%d%n%d%n", a, b, c, d);

The %d is a placeholder for a number like int. It replaces all placeholders by the arguments listed afterwards.

Here is a full list of all parameters the method accepts: Formatter-Syntax


If you look for a Java 8 solution using Streams and Lambdas, then you could use this code (documentation):

IntStream.of(a, b, c, d).forEachOrdered(System.out::println);

It first creates a stream (something like a container in this case) holding your variables and then calls the print method on each of its elements. Note that the code itself calls the print method now 4 times, not only one time. But it looks quite compact.

You can view it "equivalent" to the following code:

int[] values = new int[]{a, b, c, d};

for (int value : values) {
    System.out.println(value);
}
4
Klitos Kyriacou On

Don't just use "\n" which is a linefeed character. This is platform-dependent and if you look at the output containing lines separated by "\n" on Windows Notepad, you will not see what you expect.

Use this instead:

System.out.printf("%s%n%s%n%s%n%s%n", a, b, c, d);

Alternatively:

IntStream.of(a, b, c, d).forEach(System.out::println);
0
Luple On

You will want to use the newline character. In java it is \n
To print them using what you have, it would be:

System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
0
Erwin Bolwidt On

With System.out.format you can output the system-dependent line separator using the %n format; you can also add further number formatting to the format string.

 System.out.format("%d%n%d%n%d%n%d%n", a, b, c, d);
0
Nitesh On

You can also opt for using System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)

0
Patrick Parker On

This satisfies your stated requirement of a single call to System.out.println, uses a platform independent line separator, and avoids repetitively typing the same concatenation operation:

   System.out.println(IntStream.of(a,b,c,d).mapToObj(Integer::toString)
           .collect(Collectors.joining(System.lineSeparator())));

Here's a more generic version that works for any objects, not only ints:

   System.out.println(Stream.of(a,b,c,d).map(Object::toString)
           .collect(Collectors.joining(System.lineSeparator())));
0
Hamid Shah On

There are different print statement in java

1.println

System.out.println(1+"\n"+2+"\n"+3+"\n"+4);

2.print

System.out.print("\n"+1); System.out.print("\n"+2); System.out.print("\n"+3); System.out.print("\n"+4);

3.printf

System.out.printf("%d%n%d%n%d%n%d%n",1,2,3,4);

where %d and %n is called format specifier, they specify which type of data will be used in the printf method. thank you.