1

Firstly, I'm sorry for my bad english. I wrote my problem in the forums that speak my language. But I did not get an answer. I hope you can help me.

When I write the for loop, the System.out.println() or JTextArea.setText() command starts to work when the loop is over. Part of my codes:

for(int pixelCount = 0;pixelCount<pixelLength;pixelCount++){

  System.out.println("Progress..:"+pixelCount+"/"+pixelLength);



  int x = pixelCount%Image.getWidth();
  int y = pixelCount/Image.getWidth();
  if(isChange==1){
    if(new Color(Image.getRGB(x, y)).getRed()%2==0){
        Image.setRGB(x, y,new Color(new Color(Image.getRGB(x, y)).getRed()+1,
                                    new Color(Image.getRGB(x, y)).getGreen(),
                                    new Color(Image.getRGB(x, y)).getBlue(),
                                    new Color(Image.getRGB(x, y)).getAlpha()).getRGB());}
}

The loop is sometimes very long. So I print it on the screen to see the progress of the loop. The loop starts when I press the button. But when I press the button, the System.out.println() command looks afterwards.

EDIT: How should I write the progress in the loop?

EDIT: I solved the problem, friends. The System.out.println () command works fine. There is a replace command before the for loop starts. The program spends time there before the for loop starts. Thanks everyone who took the time and helped me

Salihcan
  • 91
  • 13
  • 2
    Possible duplicate of [Synchronization and System.out.println](http://stackoverflow.com/questions/9459657/synchronization-and-system-out-println) – Guy Jan 26 '17 at 13:14
  • @Guy there is no synchronization or multithreaded but in the OP's code so your duplicate is not relevant here – Erwin Bolwidt Jan 26 '17 at 13:19
  • @ErwinBolwidt there are probably more accurate duplicates, but the idea is that `System.out.println` is not synchronized, that's why the output appears only after the loop is over. – Guy Jan 26 '17 at 13:27
  • 2
    @Guy There is no threading involved. The fact that System.out.println is not synchronized has absolutely zero to do with the OP's problem. – Erwin Bolwidt Jan 26 '17 at 14:14

2 Answers2

2

Either call System.out.flush() after you print, or use System.err.println.

System.out is a buffered stream that prints when the buffer is full, when you flush it, or st other times that your system finds useful. System.err is unbuffered and is also the preferred stream for debug and diagnostic information, such as progress information.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Thanks for your comment. I tried both. But It's still not working. Only the System.err.println () command was red. – Salihcan Jan 26 '17 at 13:37
2

Using of System.our.flush() was already mentionned by Erwin Bolwidt. The way output works strongly depends on type of OS (and terminal) you use. For example for *nix systems you could consider some kind of library similar to Curses (it is originally a C library, but I'm sure there are some Java implementations).