0

Possible Duplicate:
Using Java, how do I create a console application which manipulates text in system.out?

I have absolutely no idea how to ask this question. I guarantee it's out there somewhere if one did a Google search (ironic that I don't know how to search for it).

I know that you can output data in Java. An example would be to use System.out.println. If you wanted to print the update value, use that statement again and it prints a new line with the changed value. I don't want to keep using print/println every time it changes. That would create a ton of statements. I'm looking for that one line to be refreshed. So say it refreshes every second to display a new value if there is one.

Value of x is: 5

Now, it is constantly refreshing and the number will change if it has been changed (so the number 6 will replace 5 in the exact same location on the screen 5 used to be sitting).

Hopefully I've made this clear enough. I went through a Java programming course in college and didn't see this (not that courses cover every possible angle).

Community
  • 1
  • 1
Tyler Montney
  • 1,402
  • 1
  • 17
  • 25
  • 1
    I think your best option is to 'fake' clearing the screen, so either print a bunch of /n or print a /f(form feed). You could also use Runtime.exec("cls"); (if you're running on Windows), but that would make the app system specific. – Jay Oct 10 '12 at 07:42
  • What you're talking about is called terminal I/O. That being said, I have no idea how to do it in Java. – CrazyCasta Oct 10 '12 at 07:44
  • 1
    Solution to your problem depends on terminal to which you print your output. So... Where would you like to display your output? Windows command prompt? Linux terminal? Other? – Lauri Oct 10 '12 at 07:44
  • Would like to display in Windows command prompt. For instance, if my app was a clock, I would not want the current time (every second) to print a new line. Current time: 1:15:30 Current time: 1:15:31 Current time: 1:15:32 I would like the current time to be overwritten and updated with the newest time. – Tyler Montney Oct 10 '12 at 07:56

3 Answers3

2

You can't really do that with Java out-of-the box. The way Java interacts with the console is almost exclusively through streams. You can only ever append stuff to a (non-rewindable) stream, never overwrite or remove from it.

There are a few exceptions in the case of the console, mostly found in the Console class, which allows a small amount of direct manipulation but doesn't allow rewriting existing text either.

You can do any of those things:

  • simply print the line again, leaving the old one above it
  • use a library like JCurses to take direct control of the output window
  • try to clear the console using an external command (cls or clear, depending on your OS)

All but the first option require a medium amount of effort and add considerable complexity to your code, so you should only consider them if that feature is absolutely required.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

If you output to command prompt, you won't be able to update the value.

In a graphical environment you can try to set a timer that checks the old value against the updated value, and if they don't match, update the label.

Or make a thread run to check the values.

voluminat0
  • 866
  • 2
  • 11
  • 21
0

If I understand correctly you want to avoid invoking println multiple times.

Write an update method and inside the update method put a println statement. So every time you invoke an update the new value of the variable will be printed.

kostas
  • 1,959
  • 1
  • 24
  • 43