3

I'm creating a java application that at some point during execution needs to display several outputs on a single line by refreshing. It will need to show each output the wait for the user to confirm. I'm stuck on how to pause the output until enter is pressed. By using the following methond:

System.out.print("Test 1\r");
System.in.read();
System.out.print("Test 2\r");

however when I use this method the cursor jumps down to a new line, like so

Test 1
Test 2

Does anyone know an easy work around for this?

4
  • What did you press to continue? Commented Dec 6, 2013 at 7:01
  • you only can press enter to continue. Commented Dec 6, 2013 at 7:05
  • Are you using Eclipse? Commented Dec 6, 2013 at 7:20
  • I'm using eclipse just to code it but OSX terminal is the target environment for the application. Commented Dec 6, 2013 at 15:18

4 Answers 4

2

My solution was to use ANSI since it's supported by OSX Terminal. After my scanner.next() I printed a: "1A" ANSI character to move the cursor up one line then used the "\r" to return the line to the beginning of the row. Here's how the code would look:

for(int i = 0; i<numTimesToRepeat; i++){    
    System.out.print("Print line " + i);
    System.in.read();
    System.out.print("\u001b[1A");
    System.out.print("\r");
}

Thanks for all the help everyone.

Sign up to request clarification or add additional context in comments.

Comments

0

Use System.out.println() to print the output and use BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); if you want to get input from console.

1 Comment

Not what the question is. the op wants to know how to have multiple inputs on the same line.
0

Simply put

System.out.println("\nPress <Enter> to continue...");
System.in.read();

Comments

0

The only possible way is to do it with a backspace i think, but the \b didnt work in eclipse console. How to get backspace \b to work in Eclipse's console? If you didnt use Eclipse, you can try it with a backspace pls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.