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

12,876

Solution 1

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.

Solution 2

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).

Share:
12,876
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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!