5

For example, instead of printing

/  
-  
\  
/  

on a new line make it so it stays on one line and is an animation of a spinner?

ggorlen
  • 33,459
  • 6
  • 59
  • 67

4 Answers4

7

Yes, print a \b (backspace) to remove the last character. In a nutshell:

System.out.print('/');
System.out.print('\b');
System.out.print('-');
System.out.print('\b');
System.out.print('\\');
System.out.print('\b');

Note that this doesn't work in Eclipse console due to a bug. In the command console, however, it should work fine.

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • Will this work across multiple platforms? Whatever OSX uses to OpenJava on Linux and the Oracle runtime on Windows? –  Jan 18 '11 at 21:20
  • I can confirm that this works on Windows, AIX and Linux. No wording about OSX since I don't use it, but since it's Unix based, I would rather be surprised that it wouldn't work over there. I'd say, give it a try yourself. – BalusC Jan 18 '11 at 21:21
  • I tried this in an infinite loop and it prints nothing until I quit the program then prints the following pattern repeatedly," / - \". Tried it in beanshell and it went crazy. :( –  Jan 18 '11 at 21:25
1

Perhaps you should implement a command-line interface within your app. Then you could take complete control of the command-line's behavior. Libraries such as Clamshell-Cli may do the heavy lifting for you.

Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
1

If you need text cursor positioning, the solution will need to be through JNI. You will need some C cursor positioning software, which will be non portable. Curses was a popular application some 15-20 years ago.
The question is do you really need this trip to the past?

Costis Aivalis
  • 13,490
  • 3
  • 48
  • 46
  • I'm making a crypto program for one of my classes. Since it is almost all binary and text manipulation keeping it in a console would seem to keep the work needed down. –  Jan 18 '11 at 21:34
1
import java.io.*;
class Load_Animate
{
    static byte anime;
    static void animate(int i)
    {
        try
        {
            for(int j = 0 ; j<=100 ; j++)
            {
                switch(anime)
                {
                    case 1:
                        System.out.print("\r[ \\ ] :" + j + "%");
                        break;
                    case 2:
                        System.out.print("\r[ | ] :" + j + "%");
                        break;
                    case 3:
                        System.out.print("\r[ / ] :" + j + "%");
                        break;
                    default:
                        anime = 0;
                        System.out.print("\r[ - ] :" + j + "%");
                }
                anime++;
                Thread.sleep(i);
            }
        }
        catch(InterruptedException e)
        {
            System.out.println(e);
        }
    }
    public static void main(String args[]) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int i = Integer.parseInt(br.readLine());
        animate(i);
    }
}

Here is what I did I used "\r" which is a carriage return, basically pointing back to the first position in the line.