Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

How can I get my output to print in a horizontal line?

I've written a simple program that takes the integers in a positive number and reverses them. So for example, if the user inputs "12345" as their number the program displays "54321". The problem is that the output gets displayed vertically like this:

5 4 3 2 1

Here's my code for the program:

public void run() {

    println("This program reverses the digits in a number.");
    int N = readInt("Enter a positive integer.");
    while (N>0) {
        println(N%10);
        N /= 10;
    }
}

Any suggestions for getting the output to display in a horizontal line? Thanks!

1 Answer

The println (print line) method adds a line break automatically each time it runs. If you want to print things inline then you can simply use the print method instead. It works in the exact same way as println except that it does not add a line break automatically.

Thanks!