-4

Recently I working on one a simulation project. I need to show home temperature status.

    int homeTemp = 20;

    System.out.println(homeTemp + " Degree");

Example Output: 20 Degree I want to update the degree value dynamically and real-time in the terminal after running the code.

Trying to show this more realistic and similative way and I want to change that terminal output dynamically like (20 degrees> Every 5-10 second update value random(current temperature +- 5 degrees)

Is that possible to do this in Java?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kadir
  • 1
  • 3
  • Does this answer your question? [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – dossy Jan 12 '21 at 15:49
  • Unfortunately. Not looking for simply generate a random number. Looking "Update Terminal Value Real-Time with Random Number" – Kadir Jan 12 '21 at 15:51
  • Then you may want to edit your question to be more clear about what you're asking, because the question you've asked is how to generate a random number every 5-10 seconds. – dossy Jan 12 '21 at 15:56
  • @dossy If you read question title says "change number dynamically" and in question content "Every 5-10 second update value" Asking for update value. But still, I add the extra explanation. – Kadir Jan 12 '21 at 16:08

2 Answers2

1

It's way more complicated than you'd think. There isn't really such a thing as a 'terminal' to java. There is a stream of bytes that flow into the process, and a stream of bytes that flow out. If you write, for example:

java -jar someapp.jar >somefile.txt

then it starts to make sense that 'update a value' becomes impossible (yes, you can overwrite bytes in a file, but that >somefile.txt could also be >/dev/printer, and you can't exactly ask the printer to grow legs, walk over to the desk of the gal who just grabbed the paper out of it, swallow the paper back in, and unprint what it printed before.

Various terminals have 'rich support' and have certain byte sequences that don't print as characters but affect it in some other way: Move the 'cursor' around, make the background of any future text to be shown blue, clear the screen, etcetera.

That's how one would do this: Send these escape codes (Print them). Both the codes you need to send, as well as figuring out if the thing you are printing to even supports them, is OS dependent and are there are a lot of options, thus, java doesn't ship out of the box with a library that tries to sail this mess.

But, good news!

They do exist! Lanterna for example. You'd have to add the jar to your project (or add the group/artifact/version to your dependencies list if you use e.g. maven, gradle, or some other build system that takes care of this for you).

Alternatively, don't make a terminal app but a swing (desktop, graphical user interface) one, where updating a JTextField or what not is trivial.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I think you overthought what he's trying to do? Sounded like he just wants the application to System.out a random number every 5-10 seconds with "Degrees" appended to it. –  Jan 12 '21 at 15:34
  • First of all, thank you for the clean response. I'm on the learning process and this writing is so educational. I'll follow the information you gave. – Kadir Jan 12 '21 at 15:34
  • @RandomCoder_01 The 'dynamically' in the title rather strongly suggested to me they want to have a single line someplace whose characters update every few seconds - not to print an endless stream. It _seems_ simple and obvious that one should be able to do that as a new java coder. Or, at least, I recall I thought so when I started out. Bear with me while I try to exercise my 'act like a fresh programmer' muscles. – rzwitserloot Jan 12 '21 at 15:51
  • @rzwitserloot I thought that's what they were asking for too. I had to read it over a few times :) And it may be the other half to what they are looking for. I'd recommend using swing over a terminal display though :) –  Jan 12 '21 at 15:55
  • @rzwitserloot Yes, you are correct. That's a just challenging thing when I start learning Java. I didn't start GUI yet, that's why I looking for a terminal solution first. Again, thank you for your great and clean response. – Kadir Jan 12 '21 at 16:07
0

Is that possible to that in Java? Thank you

Yes. You're welcome :)

Learn how to use threads and the runnable interface as well as how to keep track of time in threads)

The internet in general (and this site... you should really do a search first) will have all the info you need.

  • Thank you for your answer, but I'm looking real-time value update with randomly generated numbers. And I want to do this directly in Intellij Idea > Run > Output. Because I just learning Java and trying to figure out things. Didn't started GUI part yet. That's why I asking this in IntelliJ terminal. I tried to research this but didn't find an answer to my question, the last thing I opened this thread. @rzwitserloot explained my question actually with my "reason" to think this feature. Simply I'm new :) – Kadir Jan 12 '21 at 16:15