Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

is it possible to override the last System.out.println output, so i can for example visualize changes in a array or create a progessbar?

For example if i have this class:

class Main{
  public static void main(String[] args){
    for(int i = 0; i < 10; i++){
      for(int j = 0; j < i; j++){
        System.out.print("#");
      }
      System.out.println("");
    }
  }
}

What do i have to do to create this simple progressbar which is shown in a single line and not in 10 seperate lines?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

This works on my particular console (Windows) but it's not terribly portable...

public class Test {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.print("#");
            if (i % 20 == 0) {
                System.out.print("
                    
");
            }
            System.out.flush();
            Thread.sleep(100);
        }
    }
}

There's also the Console class, but that doesn't actually buy you very much as far as I can see...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...