1

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.

asked Oct 29, 2010 at 16:34
4
  • 3
    Is that the entire code? Any chance there's a stray line elsewhere that's providing that last output? Commented Oct 29, 2010 at 16:38
  • 2
    At last a "doesn't work on my machine" syndrom:) Commented Oct 29, 2010 at 16:39
  • 1
    have you tried executing the same code on the command line? at least that would tell you if it is NB or your system at fault. Commented Oct 29, 2010 at 16:48
  • FWIW, I'm on NB 6.9 (on Linux w/ Java 1.6) and don't see this. Commented Oct 29, 2010 at 17:41

2 Answers 2

2

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?

answered Oct 29, 2010 at 16:38
Sign up to request clarification or add additional context in comments.

1 Comment

I did think about a buffer flushing myself but don't know enough about Java to volunteer that.
0

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");

answered Oct 29, 2010 at 16:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.