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.
-
3Is that the entire code? Any chance there's a stray line elsewhere that's providing that last output?Lazarus– Lazarus2010年10月29日 16:38:12 +00:00Commented Oct 29, 2010 at 16:38
-
2At last a "doesn't work on my machine" syndrom:)Petar Minchev– Petar Minchev2010年10月29日 16:39:47 +00:00Commented Oct 29, 2010 at 16:39
-
1have 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.anirvan– anirvan2010年10月29日 16:48:54 +00:00Commented Oct 29, 2010 at 16:48
-
FWIW, I'm on NB 6.9 (on Linux w/ Java 1.6) and don't see this.GreenMatt– GreenMatt2010年10月29日 17:41:32 +00:00Commented Oct 29, 2010 at 17:41
2 Answers 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?
1 Comment
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");