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
  • 44,755
  • 7
  • 76
  • 106

4 Answers4

8

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,082,665
  • 372
  • 3,610
  • 3,555
  • 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
2
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.

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
  • 303,325
  • 100
  • 852
  • 1,154
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,680
  • 3
  • 46
  • 47
  • 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