views:

244

answers:

3
System.out.print("My string: ");

My string: BUILD SUCCESSFUL (total time: 1 second)

System.out.print("My string  ");

My string BUILD SUCCESSFUL (total time: 1 second)

System.out.print("My string:  ");

My string: My string:
BUILD SUCCESSFUL (total time: 1 second)

Printing a string that ends with a colon and two spaces will make it print twice. What is happening here?


Edit: looks like it's a bug in Netbeans indeed

http://netbeans.org/bugzilla/show%5Fbug.cgi?id=167279

+2  A: 

I think the output stream's buffering is making you interpret what you see wrongly. Try using println().

unwind
Or add a call to flush after the call to print.
bmargulies
IIRC, `System.out` autoflushes like crazy.
Tom Hawtin - tackline
Looks like println() only prints it once.
Stijn
@Tom Hawtin. It only autoflushes complete lines.
Alexander Pogrebnyak
A: 

I noticed that behavior on Netbeans, when running JUnit tests. It that also your case? Seems to be just a bug.

java.is.for.desktop
Yes, using Netbeans.
Stijn
<eclipse-fanboy>Ah, one more reason not to use Netbeans.</eclipse-fanboy> ;)
BalusC
BalusC: org.xml.sax.SAXParseException: Content is not allowed in prolog. (gaah!!)
Esko
A: 

Which java version do you have? It doesn't happen in 1.6.0_16

Are you sure it's not some danging lines in the unix shell (if you're on unix)?. Try with Systems.out.println("My string: ");

Thrawn
See my edit, it seems to be a confirmed bug and the fix will be there in a future patch.
Stijn