Home > other >  How do I print the output in a straight vertical line from for loop
How do I print the output in a straight vertical line from for loop

Time:11-25

I used a 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 output is:

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

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