System.out.println, System.err.println

I have this code:

 System.err.print("number of terms =   ");
 System.out.println(allTerms.size());
 System.err.print("number of documents =   ");
 System.out.print("number of documents =   ");

I think the results should be like this:

number of terms = 10
number of documents =1000

but the results were

10
1000
number of terms =   number of documents =

Why and how to solve it?

+3
source share
7 answers

To solve the problem

System.out.print("number of terms = ");

System.out.println(allTerms.size());

System.out.print("number of documents = ");

System.out.print("number of documents = ");

System.out.println -> Sends output to standard output. Usually a monitor.

System.err.println -> Sends output to the standard error stream. Usually a monitor.

+3
source

The out and err streams are independent.

To get the desired result, you must clear the threads or just use only one thread for all outputs.

+9
source
System.out.print ("He");  
System.out.print ("llo ");  
System.out.println ("World");  

"Hello World",

System.out.print ("He");  
System.err.print ("llo ");  
System.out.println ("World");  

"llo He World" "HeWorld llo". 2 .

+3

System.err.print() stderr, . System.out.print()

+2

System.err System.ou - . System.out stdout system.err, stderr.

System.err System.out, , .

:

System.err.print("number of terms = ");

System.out.print("number of terms = ");

println, : System.out.println?

+2

print * . .

System.err.print("number of terms =   "); System.err.flush();  
System.out.println(allTerms.size()); System.out.flush();  
System.err.print("number of documents =   "); System.err.flush();  
System.out.print( numberOfDocuments ); System.out.flush();  

:

number of terms =   10
number of documents =   1000

, , , , err .

+1

This may be the reason: errand outthey have different output streams that will be printed according to the sequence of access to the console.

0
source

All Articles