0

I want to print integer in new line.
like

 1
 2
 3
 ..

how I do that with System.out.println(a,b,c) // here comma used only for explain this. Can you help me?

System.out.println(a,b,c)
Andreas
  • 154,647
  • 11
  • 152
  • 247
Shakib Ul
  • 1
  • 1
  • 1
  • Please read: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) and [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Turing85 Aug 23 '19 at 19:58
  • 1
  • loop it through an unknown length of iteration and print the index (as in i in iteration index) on screen. `for(int i=0;i – apollo Aug 23 '19 at 20:10

5 Answers5

3

Here's an example using Java 9+ functionality to do this (do note that it creates an extra List, so don't do this for too many objects):

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

If you have trouble with the types, then you can do the following:

List.<Object>of(a, b, c).forEach(System.out::println);

In Java 8, you can do the following instead:

Arrays.asList(a, b, c).forEach(System.out::println);
//If this doesn't work, do this:
Arrays.asList(a, b, c).forEach(i -> System.out.println(i));
//If the types don't work, do this:
Arrays.<Object>asList(a, b, c).forEach(System.out::println);
//If none of them work, do this:
Arrays.<Object>asList(a, b, c).forEach(i -> System.out.println(i));

Here's an example using String.format, which doesn't create any extra lists:

System.out.println(String.format("%d\n%d\n%d\n", a, b, c));

You can improve this by directly using System.out.format, like so:

System.out.format("%d\n%d\n%d\n", a, b, c);
Avi
  • 2,611
  • 1
  • 14
  • 26
  • 5
    If you are using `String.format` inside of a `print` statement, you might as well use `System.out.format` or `System.out.printf`. – Nexevis Aug 23 '19 at 20:08
  • @Nexevis I'm never going to have to type String.format again, thanks :D – Avi Aug 23 '19 at 20:13
1

You either use 3 calls to println​(int x):

System.out.println(a);
System.out.println(b);
System.out.println(c);

Or a single call to println​(String x), using lineSeparator() and string concatenation:

System.out.println(a + System.lineSeparator() + b + System.lineSeparator() + c);

Or a single call to printf: (I recommended this one)

System.out.printf("%d%n%d%n%d%n", a, b, c);
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

You can create a method using vargars:

public static void printIntegers(int... ints){
    for(int i: ints) System.out.println(i);
}

And use that method to print all integers you need:

printIntegers(1,2,3,4);
Jorge Garcia
  • 65
  • 1
  • 7
0

if you wanna print in a sequence from x-y then you can use this method A)

for(int i=x;i<=y;i++)
{   System.out.println(i); 
 }

or if you want to print three nums like 1 ,2 and 3 then:

System.out.println(1);
System.out.println(2);
System.out.println(3);
0

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

\n means new line.

RIVERMAN2010
  • 427
  • 3
  • 9