user889494
user889494

Reputation:

Java - Is it possible to keep printing text on the same line you enter a number on?

I want to be able to enter a number using nextInt() and then print text on the same line as the number I entered. For example...

Scanner scan = new Scanner(System.in);
System.out.print("Enter your number: ");
int x = scan.nextInt(); //After you press 'enter' it goes to the next line and keeps printing text
System.out.print("I want this on the same line as my number!");

And the output would look something like this:

Enter your number: 4356
I want this on the same line as my number!

Is there a way I can get the text to print right after the number? So it would look something like this?...

Enter your number: 4356 I want this on the same line as my number!

Upvotes: 2

Views: 13124

Answers (2)

adarshr
adarshr

Reputation: 62573

Have you tried

System.out.print("\rI want this on the same line as my number!");

But note that because the user has to press the Enter key, there's no way (AFAIK) to go a line backwards and erase till the beginning of it.

Upvotes: 2

Luigi Plinge
Luigi Plinge

Reputation: 51109

On Windows, I'm pretty sure the answer is "no", because there doesn't seem to be any reverse line feed (see this question: Reverse line feed in Windows / Java).

Upvotes: 2

Related Questions