Home > Software engineering >  How do i print the output in a straight vertical line from for loop (in java)
How do i print the output in a straight vertical line from for loop (in java)

Time:11-23

So i used for loop in my code and my output prints vertically but what i want is it to print horizontally for example

`for (int i = 0; i < 10; i ) { System.out.println("i is " (i 1) " | "); }

for this the out put is :

i is 1 | i is 2 | i is 3 | and so on...

But the output i want is :

i is 1 | i is 2 | i is 3 | ... and so on`

`for (int i = 0; i < 10; i   ) {
      System.out.println("i is "   (i 1)   " | ");
}

for this the out put is :

i is 1 |
i is 2 |
i is 3 |
and so on... 

But the output i want is :

i is 1 | i is 2 | i is 3 | ... and so on`

CodePudding user response:

Change it to System.out.print as below:

for (int i = 0; i < 10; i   ) {
      System.out.print("i is "   (i 1)   " | ");
}

That should do.

CodePudding user response:

use only print() function instead of println()

follow below code: `for (int i = 0; i < 10; i ) { System.out.print("i is " (i 1) " | "); }

CodePudding user response:

Use System.out.print instead of System.out.println because println prints at new line.

for (int i = 0; i < 10; i  ) {
        System.out.print("i is "   (i   1)   " | ");
}

CodePudding user response:

Println prints a new line while print does not

  • Related