1

I tried to make a Simple Clock in Java

My Expected Output was 00:00:00 // Showing Time

My Actual Output Was

00:00:00
00:00:01
00:00:02
.......

I tried many things and was unable to clear previous output after being printed

Thanks for Reading, Hope somebody will give a valuable feedback

Code:

package com.company.Time;

import java.util.Scanner;

import java.util.Date;


public class Main {

    



    public static void main(String[] args) {
        System.out.println("Enter Time");
        System.out.println("Enter Hour :");
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();

        System.out.println("Enter Minutes :");
        Scanner sc2 = new Scanner(System.in);
        int m = sc2.nextInt();

        System.out.println("Enter Seconds :");
        Scanner sc3 = new Scanner(System.in);
        int s = sc3.nextInt();



        int i = 0;

        try {
            while (i < 1)
            {
                for (; h < 24; h++)
                {

                    for (; m < 60; m++)
                    {
                        for (; s < 60; s++)
                        {
                            System.out.println(h + ":" + m + ":" + s);
                            System.out.print("\u000C");
                            Thread.sleep(1000);

                        }

                        s = 0;

                    }
                    m = 0;
                }
            }
        }

        catch(Exception exception)
        {
            System.out.println("Ushh");
        }

    }
}
Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • What is the problem exactly? – Renis1235 Jan 14 '22 at 07:37
  • Java's console support is rudimentary, it doesn't, by default, support things like "clearing" or cursor re-positioning. You might be able to fake it by using a backspace character for [example](https://stackoverflow.com/questions/15843202/how-to-show-percentage-progress-of-a-downloading-file-in-java-consolewithout-ui/15846788#15846788) – MadProgrammer Jan 14 '22 at 07:40

3 Answers3

1

Replace this:

System.out.println(h + ":" + m + ":" + s);
System.out.print("\u000C");

With this:

System.out.print(h + ":" + m + ":" + s + "\r");

The carriage return symbol (\r) returns the cursor at the start of the line. You must use print() and not println() for it to work. If the program jumps to a new line, the previous line is left as is.

Renis1235
  • 4,116
  • 3
  • 15
  • 27
0
public static void main(String[] args) {
        System.out.println("Enter Time");
        System.out.println("Enter Hour :");
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();

        System.out.println("Enter Minutes :");
        Scanner sc2 = new Scanner(System.in);
        int m = sc2.nextInt();

        System.out.println("Enter Seconds :");
        Scanner sc3 = new Scanner(System.in);
        int s = sc3.nextInt();



        int i = 0;

        try {
            while (i < 1)
            {
                for (; h < 24; h++)
                {

                    System.out.print(h+":");
                    for (; m < 60; m++)
                    {
                        System.out.print(m+":");
                        for (; s < 60; s++)
                        {
                            System.out.print(h + ":" + m + ":" + s);
                            Thread.sleep(1000);
                            System.out.print("\r");
                        }

                        s = 0;

                    }
                    m = 0;
                }
            }
        }

        catch(Exception exception)
        {
            System.out.println("Ushh");
        }

    }

use '\r'

chasing fish
  • 114
  • 3
0

Form feeds (the \u..C character) requires the terminal Emulator to do the clearing of the screen. This is common on Linux, but the standard one on Windows does not support this.

You may want to use an emulator that does. I believe the new Windows Terminal in the AppStore does.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347