简体   繁体   中英

System.out.println() doesn't work as i want in the for loop

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

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM