0
class progReport extends Thread {

    // function for clearing the line by printing escape char '\b'
    private static void cls(){
        for (int i = 0; i <50; i++) {
            System.out.print("\b");
        }
    } //End of cls

    //Running a thread for printing the message every 100 ms.
    public void run(int prog) {
        int k;
        cls();

        try { 
            Thread.sleep(100);
        } catch(InterruptedException e) { 
            e.printStackTrace(); 
        }

        System.out.print("Scanning: " + (prog) + "%      ");
        k=prog%3;   // mod 3, so as to decide which symbol to be used for revolving animation

        switch (k) {
            case 0: 
                System.out.print("/");
                break;
            case 1: 
                System.out.print("--");  // LINE WITH THE PROBLEM
                break;
            default: 
                System.out.print("\\");
                break;
        } // End of switch
    } // End of run
} // end of class progReport

    // class with main
public class ProgressDemo {
    public static void main(String...a) {
        int prog = 0;
        progReport pr = new progReport();

        while(prog <= 100) 
            pr.run(prog++);

        System.out.println("\nScanning Complete!!");
    } // End of main
} // End of class ProgressDemo

Upon executing this code on the command line, it doesn't clear the last "-" of the case 1: in which two dash are printed "--", but if I include a single space in the other cases(i.e. changing "/" and "\\" to " /" and " \\") then it is clearing the whole line properly. Can someone help me point out why is it behaving so weird?

hackrush
  • 88
  • 9

1 Answers1

1

Hmm. This is an interesting problem. The issue you encountered is that the backspace character is just that. A character. And what to do with this character is up to the terminal which is printing it.

So in my case I am using an OS X terminal, and this program simply takes the backspace character and moves the cursor one slot backward. Then, if there are follow up characters, then it deletes those characters.

So when you print:

System.out.println("Hello\b");

Nothing happens. Because there are no follow up characters. On the other hand when you print:

System.out.println("Hello\b ");

Then it deletes the "o" character because there was a follow-up character (an empty space). I know that it's weird but this is how it is.

Now in your program you basically print 50 backspace characters into the output stream, then you print your message. Then 50 backspace again. Then another message. Then 50 backspace again. Then your third message and so on.

And the length of your messages are:

*1 N

*2 N+1

*3 N

In the second case the length of your message is one character longer than the other, so even though you print N+1 characters, only N backspace characters will be active, because the backspace characters will look at your last message not the follow-up message.

Putting these two things together (look behind in your code, and the terminal moving the cursor but not deleting it) you will experience what you experience.

One solution is that you simply append an empty space in the beginning of your cls function like this:

// function for clearing the line by printing escape char '\b'
private static void cls(){
    System.out.print(" ");
    for (int i = 0; i < 50; i++) {
        System.out.print("\b");
    }
} //End of cls

This will ensure that even when your message is only N long, it will activate N+1 backspaces.

Anyway, your program is not running in a new thread at all. You should study multi-threading also, to learn how to properly start and use threads.

For starter when you extend the Thread class, you should run the start() method to activate it. Otherwise your code is just running on the main thread.

Alma Alma
  • 1,641
  • 2
  • 16
  • 19
  • Thanks, it helped a lot. Also thank you for pointing out the thread start() issue, I am new to multithreading. It would be useful if you could recommend some good, detailed online material for threads. – hackrush Jun 30 '16 at 19:37
  • Well, this is a difficult question. I was struggling finding a good source to learn about threading online. Of course you should follow the Oracle Threading tutorial first, so you will understand the basic concepts. https://docs.oracle.com/javase/tutorial/essential/concurrency/ But I would suggest to pick up this book: "Java concurrency in practice" written by Brian Goetz. It's an excellent book full of examples and counter-examples (what you should and shouldn't do) and it explains fundamental concepts in a very easy to read style. – Alma Alma Jun 30 '16 at 21:41
  • 1
    Aye aye, Captain, thanks for these brilliant recommendations, and I apologize if my request for online source took up your time. And once again thanks a lot for helping, you do a great job. :-) – hackrush Jul 01 '16 at 05:19