1

I am trying to make a 24 hour clock and I want it to increment seconds, minutes, hours in a fixed place like within the Command Shell this.

00:00:03

previous lines are replaced instead of adding lines below like this:

00:00:01
00:00:02
00:00:03

How to make something like this? Here is my code

Clock myClock = new Clock();
       
while(true) {
   Scanner sc = new Scanner(System.in);    
       
   System.out.println("Enter ticks");
   String ticks = sc.nextLine();
   int input = Integer.parseInt(ticks);
             
   for(int i = 0; i < input ; i++) {            
       System.out.println(myClock.ToTimeFormat());
       myClock.Tick();
       Thread.sleep(100);              
   } 
}

Thanks for help in advance

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
Karim Aly
  • 17
  • 4
  • 1) I can't really understand what exactly you are asking and 2) you haven't provided the code for your `Clock`, so how are people supposed to help you? What do you mean when you say "_increment in one place_"? – maloomeister Oct 29 '21 at 05:53
  • What I need is that when I run the program I want the clock to count like this 00:00:23 but in my code the clock count like this 00:00:01 00:00:02 and so on – Karim Aly Oct 29 '21 at 05:56
  • I don't think the clock class will add something – Karim Aly Oct 29 '21 at 05:57
  • java does not support command line funktions as under c - you can only try to clean the screen by using the default windows command (`cls`)... (assuming you are under a windows OS) – Martin Frank Oct 29 '21 at 06:19
  • see [this article](https://stackoverflow.com/questions/7112259/how-to-execute-windows-commands-using-java-change-network-settings) for further details – Martin Frank Oct 29 '21 at 06:52
  • you could see this thread for another soultion idea https://stackoverflow.com/questions/1001335/java-gotoxyx-y-for-console-applications – Martin Frank Oct 29 '21 at 06:57

1 Answers1

0

Try this:

for(int i = 0; i < input ; i++) {            
       System.out.println("\r" + myClock.ToTimeFormat());
       myClock.Tick();
       Thread.sleep(100);              
   } 

\r returns the start of the line, the problem is that it works on some terminals/ and it does not in others. Try it out.

Renis1235
  • 4,116
  • 3
  • 15
  • 27