Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:11:04+00:00 2026-06-08T22:11:04+00:00

I’m new to Java and as an attempt to learn more I tried making

  • 0

I’m new to Java and as an attempt to learn more I tried making a clock. It works perfectly, except for the fact that it prints on a new line every time a second changes. How do I make it so that I can replace the text that’s already been printed out with the new time?

public class test {
    public static void main(String[] args) {
        test.clock();
    }
    public static void clock() {
        int sec = 0;
        int min = 0;
        int h = 0;
        for(;;) {
            sec++;
            if(sec == 60) {
                min++;
                sec = 0;
            } else if(min == 60) {
                h++;
                min = 0;
            } else if(h == 24) {
                h = 0;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(h < 10) {
                System.out.print("0"+h+":");
            } else {
                System.out.print(h+":");
            }
            if(min < 10) {
                System.out.print("0"+min+":");
            } else {
                System.out.print(min+":");
            }
            if(sec < 10) {
                System.out.println("0"+sec);
            } else {
                System.out.println(sec);
            }
        }       
    }
}

Here’s my code. And here’s an example of what I’m trying to do:
I want to turn this: 00:00:00,
into this: 00:00:01 (the code does this but it prints on a new line and doesn’t delete the old time).
The thing is that I want to get rid of the first one (00:00:00) and on the same line print the second one (00:00:01).

Is this possible in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:11:06+00:00Added an answer on June 8, 2026 at 10:11 pm

    That has nothing to do with java actually. Rather you want the console you are displaying in to overwrite the text.

    This can be done (in any language) by sending a carriage return (\r in Java) instead of a linefeed (\n in Java). So you never use println(), only print() if you want to do that. If you add a print(“\r”) before the very first print it should do what you want.

    BTW it would be far more elegant and simple if you built the entire time in a string and then output it all at once 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.