views:

136

answers:

2

I have the following lines :

    for (int i=0;i<3;i++) System.out.print("\nA : "+i);
//    System.out.println("");
//    for (int i=0;i<3;i++) System.out.println("B : "+i);

The output is :

A : 0
A : 1
A : 2A : 2

Why ? I expected this :

A : 0
A : 1
A : 2

But if I uncomment the 2nd and 3rd lines [ together or one at a time ], it behaved correctly ? What's going on ? Is it my PC problem, or my NB6.7 problem ? I can't believe Java would do this !

Edit :

 for (int i=0;i<3;i++) System.out.print("A: "+i+"\n")

works correctly as expected.

When I ran it from command line, no problem at all, seems like a NB problem.

+2  A: 

Works for me:

public class Test {
    public static void main(String [] args) {
        for (int i=0;i<3;i++) System.out.print("\nA : "+i);
    }
}

Output:

C:\Users\Jon\Test>javac Test.java

C:\Users\Jon\Test>java Test

A : 0
A : 1
A : 2
C:\Users\Jon\Test>

Perhaps Netbeans is just echoing the last line of output when the app terminates?

Jon Skeet
I did think about a buffer flushing myself but don't know enough about Java to volunteer that.
Lazarus
A: 

It works for me as well, but try the following and tell us what it does. Try putting the newline after i.e. print("A: "+i+"\n");

Soldier.moth
print("A: "+i+"\n") works correctly as expected.
Frank