Strange System.out.print behavior inside a for loop

I'm trying to create a simple ASCII progress bar in the console that would display like this:
|--------------------------------------------|
============================More equal signs would appear as time goes on. At least this is what I want to happen.
Instead, what happens is, none of the equal signs are printed until the very end, after the for loop in which the System.out.print("=") statement is in, has exited.
This is my code (probably more than necessary is shown, but the important part is the for loop):
    public void addSomeDiffys(int numOfDiffys, int minInt, int maxInt) {
        Diffy currentDiffy;
        System.out.println("\nCreating Diffys.");
        System.out.println("|--------------------------------------------|");
        for (int i=0; i < numOfDiffys; i++) {
            currentDiffy = new Diffy(minInt, maxInt);
            if ((i/(double)numOfDiffys)*100 % 2 == 0)
                System.out.print("=");
            diffyList.add(currentDiffy);           
        System.out.println("\n" + diffyList.size() + " Diffys created.\n");
        System.out.println("Sorting according to highest rating...");
        Collections.sort(diffyList,Diffy.HIGHEST_RATING_ORDER);
        System.out.println("Done.\n");
    }I also tried changing the System.out.print("="); to System.out.println("="); This time it works as expected, its just not very pretty...
I get something like this:
|--------------------------------------------|
=
=
=
=
=
=
=
=
=
=
=
=So again, when I use println, rather than print, the equal signs are printed over time, slowly, as the progress increases. When using print, Nothing happens for a long time, and then suddenly they all appear after the for loop has terminated, which makes no sense.
Thanks for any help.

When using print, Nothing happens
for a long time, and then suddenly they all appear
after the for loop has terminated, which makes no
sense.
Yes it does make sense! The characters are buffered inside the PrintStream and written when there is a full line to write. You could try a flush() after each = is written but I have no confidence that it will make any difference.

Similar Messages

  • Strange System.out.print() behaviour

    Hi guys
    While trying to determine the problem of the OP in another thread, I came across something that I find rather strange. Could someone perhaps offer an explanation as to why this is happening? [http://forums.sun.com/thread.jspa?messageID=10541658#10541658]
    Thanks and regards

    java version "1.6.0_10"
    Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
    that one works fine for me ....
    stupid maven .... took most of my day away !
    And i guess i gave you something to wonder about :)
    Edited by: stevoo on Dec 11, 2008 6:31 AM

  • How to view System.out.print() messages for adapters in OIM 9.0.3

    I have created an adaptor for OIM from a java jar file. When that adaptor executes after some updates on tables it produces some execptions/error.
    I can not see the details of these exceptions as I do not know where is the standard output located when adapter is executed via OIM.
    Where cen I see output from my code lines that contain: System.out.println()?
    thanks

    I am not sure if logging is enabled as OIM was installed previously by another person - how can I check if it is enabled and how to use log4j in this case?
    OIM Design Console window shows no messages comming from my System.out.print() statements.
    /br
    Djeno

  • System.out.println statements inside my session beans

    I have some System.out.println statements inside my session beans to track some error conditions. I would like to know where this output will be printed. Is there any text file I should look for or start up any console to see the output.
    I am running iPlanet SP3 test drive edition iPlanet Web Server 4.1 on WIndows NT machine.

    Hi,
    These statements will be printed on the kjs logs file. To view them, go to control panel-> services-> select the iPlanet Application server and click "start up" tab. Check " allow this sevice to interact with the desk top" in the service window that has been popped up. Restart the iPlanet Application server.
    View the kjs window. All your System.out.println statements will be printed out there.
    If you have any further queries please get back to the forum.
    Thanks for visiting our web forum,
    Rakesh.

  • Problem with national characters calling System.out.print(...)

    I need to develop an application printing spanish characters like "�" (ce trencada) "�", "�", etc.
    The problem is when I type
    System.out.print("�")in my application, I get "plus-minus" symbol while executing it.
    Anyone can help me, please?
    Thank you in advance.

    check the list of fonts available. You can use the following code for the purpose.
      public static void main(String args[])
            String fonts[] = getFontNames();
            for(int i = 0; i < fonts.length; i++)
                System.out.println(fonts);
    public static String[] getFontNames()
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    return ge.getAvailableFontFamilyNames();

  • J2me sdk 3.0  problems in System.out.print

    Hi
    I can not use println to print Chinese .
    First I just use System.out.print("test") to print some debug information , and I surprised to find it print nothing.
    Then I try System.out.println("test") and it worked well. But there still problem when print Chinese,
    like System.out.println("&#27979;&#35797;") and it just print a mistake string as "??" !
    Can some tell me how to solved it, thanks .

    Note: This thread was originally posted in the [Sun Java Wireless Toolkit|http://forums.sun.com/forum.jspa?forumID=82] forum, but moved to this forum for closer topic alignment.

  • Why System.out.println() is ok but System.out.print(); ?

    why System.out.println() is ok but System.out.print();?
    if i leave empty between parantheses in print .compiler gives error but println() doesnt?

    gimbal2 wrote:
    Think about it for a moment. What would System.out.print() do without any parameters? What CAN it do?That's easy: print() COULD behave like println() without writing a newline at the end ;)
    But I agree with Kayaman and Ram: javadoc is very nice, you simply have to read it.

  • Java - Eclipse debug applet with System.out.print in jboss console

    I use System.out.print to debug my java programs using Eclipse. I'm new
    with applets, and when nothing showed up in the applet window, I tried
    using print statements. I was expecting to see them in the jboss console like
    always, but there was nothing there. I tried looking this problem up
    online and found nothing. Does anyone know how I can use print
    statements to debug my applet? (Japplet to be specific).

    System.out.println works for me. The output statment are visible in the Console Window within Eclipse.

  • How to create a class like System.out.print

    Hi,
    I need to create a class like System.out.print
    Say for ex: i have a class A ..
    I need to call it A.b.c
    cis the method..
    could you tell me, how can i do this..
    AMJ

    class A{
    public static final B b=new B();
    class B{
    public String getXXX(){
    return "Callled getXXX()";
    public class C{
    public static void main(String[] args)
    System.out.println(A.b.getXXX());
    }

  • Difference between " system.out.print( ) " and " system.out.println( ) "?

    Hi frnds, i m a beginner in JAVA today only started with the complete refrence....can you help me and tell the the Difference between " system.out.print( ) " and " system.out.println( ) "?

    Rashid2753 wrote:
    hi,Yes. But it's a good idea for new Java programmers to become accustomed to using helpful resources like the API Javadocs because it's much faster then waiting for replies everytime you have a question. For experienced developers the API Javadocs are an indispensible resource.

  • System.in.read and System.out.print questions

    On the following code, two questions:
    1. the output is not what I expected. Why doesn't the System.out.println(i+" "+(char)i); print everytime i press enter a character? It seems to print all at once after everything is entered.
    package stars;
    import java.io.*;
    public class Stars {
      public static void main (String[] args) throws IOException {
        String U="";
        System.out.print("What is your name: ");
        while (true) {
          int i=System.in.read();
          U+=(char)i;
          System.out.println(i+" "+(char)i);
          System.out.flush();
          if (i==10||i==13) break;
        System.out.println("\rHello "+U);
    }C:\>java -cp . stars/Stars
    What is your name: blah
    98 b
    108 l
    97 a
    104 h
    13
    Hello blah
    2. What is the most natural way to check for the press of an enter key? Is there a java attribute or method for the "Enter key" since it is different on unix and ms-dos? Thanks

    On the following code, two questions:
    1. the output is not what I expected. Why doesn't
    the System.out.println(i+" "+(char)i); print
    everytime i press enter a character? From:
    http://scv.bu.edu/Doc/Java/tutorial/java/nutsandbolts/input.html
    ================
    "The read() method provided by System.in reads a single character and returns either the character that was read or, if there are no more characters to be read, -1.
    When a program reads from the standard input stream, the program blocks waiting for you to type something in. The program continues to wait for input until you give it some indication that the input is complete. To indicate to any program that reads from the standard input stream that you have finished entering characters, type the end-of-input character appropriate for your system at the beginning of a new line"
    ================
    In other words, Java can't do what you want to do.
    <snip>
    >
    2. What is the most natural way to check for the
    press of an enter key? Is there a java attribute or
    method for the "Enter key" since it is different on
    unix and ms-dos? Thanks
    System.getProperty("line.separator");Jim S.

  • HOW TO system.out.PRINT a curly brace: {

    How is it possible to print a {  with command: system.out.print{ "text { text"}; ?
    Thanks

    How is it possible to print a {  with command:
    system.out.print{ "text { text"}; ?
    ThanksYour command has 2 errors in it.
    1) "system" must start with a capital "S"
    2) You used curly brackets where you should have used parentheses
    This works: System.out.print( "text { text");
    (although I suspect that you really want to use println, not print)

  • How do you overwrite System.out.print()

    I am sure there is a way to over write the last entry to System.out.print ?
    I think it is by using an escape sequence (eg \t or \n), but cant work out which letter to use.
    can any one help?
    Simon

    It's \r:
    class Overwrite {
         public static void main(String[] args) {
              System.out.print("Old Text");
              System.out.print("\rNew");
    }

  • System.out.print and System.out.println

    is it true that the System.out.print("\n") will print a new line character in all platforms?
    in other words is it ok to assume that System.out.print("\n") and System.out.println(); will give the same output regardless of platform?

    System.out.print("\n") Chances are that will produce different results on different platforms as some platforms use \n, some use \r some use \n\r (or is it \r\n?).
    The question is why do you care when System.out.println() will do what you want? An alternative:
    System.out.print(System.getProperty("line.separator"));

  • How to speed up my code inside the for loops,

    how to speed up my code inside the for loops using c# WinForm.

    Hi  John,
    Please take a look at the reply from Wyck in the following thread.
    How to speed up for loop?
    His answer is very comprehensive.   Thanks.
    Best regards,
    kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

Maybe you are looking for