A student mentioned in Zhang Xiaoxiang's blog raised a strange question about IO.
A simple and strange question
A student asked a strange question about I/O. The question is: Read a character entered by the keyboard and print the output, A string is added before and after the printed characters. The code of the program is as follows:
Public class Test
{
Public static void main (String [] args) throws Exception
{
System. out. print ("Input a char :");
Char ch = (char) System. in. read ();
System. out. println ("hello" + ch + "AB ");
}
}
Compile and run this program.
(1) input the character "a". The command line window prints the output result 1, as expected.
Figure 1
(2) run the program again, press the Enter key, and print the output result 2 in the command line window. In this case, the output result is that the character string after the characters in the print statement "replaces" Some Characters in the character string before the character, that is, the output string "ABllo" is printed ".
Figure 2
Let's take a look at why the output results are normal when you enter common characters. Press the Enter key to replace the previous string, and no line breaks are output?
--------------------
The problem above is an example that I have analyzed the cause and made adjustments for the convenience of lectures, and I have written an error pattern. In fact, the original problems of the trainees were very concealed, and they were not as direct as in my case above, and they could not find the correct rules. The following is his original program code.
Public static void main (String [] args) throws Exception {
String str = "";
Char ch0 = 0;
System. out. print ("intput string and String :");
Do {
Ch0 = (char) System. in. read ();
Str + = ch0;
} While (ch0! = '#');
Char ch/* = '0 '*/;
System. out. print (str );
Ch = (char) System. in. read ();
System. out. println ("hello" + ch + 'B ');
}
-------------------------- Answer ----------------------
When reading a character entered by the keyboard, we press the Enter key on the keyboard to read two characters, namely, "/r" and "/n ". The character "/r" indicates the carriage return, that is, the cursor returns to the beginning of the current row without line breaks. The character "/n" indicates line breaks, that is, the cursor moves to the beginning of the next row of the current row.
Press the Enter key, and System. in. read () reads the character "/r". When executing the following program code:
System. out. println ("hello" + ch + "AB ");
After the string "hello" is printed, the output character "/r" is printed. Then, the cursor moves to the beginning of the row where the string "hello" is located, because there is no character "/n ", will not switch to the next line, and then print the string "AB ". In this way, the string "AB" overwrites the first two characters of the string "hello", so we can see that the output in the command line is the string "ABllo ".
--------------------- Further comment ----------------------
Yuther is published on 17:59:00 IP: 58.207.152. * This problem was not found in Eclipse. I think this is caused by the encoding scheme. In Eclipse, two bytes are read by Unicode encoding, the MS console may read the statement in one byte. I would like to discuss the encoding used by the system to read this statement. Xifo is published on 08:27:00 IP: 61.191.142. * This result depends on a specific platform.
The Windows platform processes the carriage return with "/r/n;
For Linux and Unix platforms, press ENTER for "/n/r;
As for cross-platform software such as Eclipse, the system's standard output device has been redirected. The test instruction is also based on "/n/r" and press Enter.