1

I'm trying to print on the same line inside a while loop in the eclipse terminal.

I tried the following code:

while (condition)
  {
    System.out.print("Waiting..\r")
  }

And it will just print me lines and lines of "Waiting.." until my condition.

What I want is just "Waiting" printed out on top of the previous print until my condition.

Edit:

Just to be clear, I don't want "Waiting.." printed on many lines but on the same line during my while loop.

So I do NOT want this

Waiting..
Waiting..
Waiting..
Waiting..
[...]

But THIS (No matter how many times it is called inside my while loop)

Waiting..
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
Difender
  • 495
  • 2
  • 5
  • 18
  • you would need to reset the console. my output with your code: `Waiting.. Waiting.. Waiting.. Waiting..`. each waiting is in a new line – XtremeBaumer Dec 16 '16 at 11:44
  • See http://stackoverflow.com/questions/7939802/how-can-i-print-to-the-same-line – MarkusF Dec 16 '16 at 11:44
  • @MarkusFieber This is exactly what I said. `\r` is not working for me. – Difender Dec 16 '16 at 11:47
  • then we will need more code, because the code as you posted does what you want. – XtremeBaumer Dec 16 '16 at 11:47
  • "I'm trying to print on the same line inside a while loop" and "I want ... just 'Waiting' print out on top of the previous one over and over" seem to contradict each other. You may want to clarify exactly what you intention is. Add some examples of output you are seeing and what you would like to see. – Rudi Kershaw Dec 16 '16 at 11:48
  • Are you trying to do this in eclipse? If so, see the top comment against this answer. http://stackoverflow.com/a/7939820/2182928 – Rudi Kershaw Dec 16 '16 at 11:50
  • I am trying on Eclipse indeed.. I guess this is the answer I was looking for then x.x – Difender Dec 16 '16 at 11:51

1 Answers1

1

As mentioned in the first comment for this answer there is a bug in the eclipse terminal which prevents the \r character from working correctly.

Your code is actually fine, it just doesn't appear to work in eclipse. Also, as a side note, generally the \r character should be at the beginning of the print statement.

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77