How do I get output on same line

Joshua Paige :

I need to write a program where the output will be on the same line as the input. I have tried print() and println() but they come out the same. What do I need to add?

String str= scanner.next();
System.out.println("    donut");

the output is (if I were to type in apple)

apple
    donut

but I want it to print

apple   donut
lealceldeiro :

You cannot simply do that. The line where the Scanner is waiting for an input is not used for printing. Once you press Enter, the position in the console moves to the next line, where is printed what you put inside (print, println, etc).

This is a different matter, not related to whether you want to print some tokens in the same line or not, in which case using System.out.print/println would solve the problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=92794&siteId=1