I was troubled by a small problem when I learned how to input data in the Java console. The source code is as follows:
Import Java. io. ioexception; public class test {public static void main (string Arg []) throws ioexception, numberformatexception {testing ();} public static void testing () throws ioexception, numberformatexception {byte [] B = new byte [1024]; int num = system. in. read (B); string S = new string (B, 0, num); try {int n = integer. parseint (s); system. out. println (n);} catch (numberformatexception e) {system. out. println ("erro R! ");}}}
SectionCodeConvert the received data from the console to the string format and then to the int type output. However, no matter what the input is, the Code always outputs an error!
As a result, I think that when I learned the c \ c ++ language, the input buffer will store one more carriage return character, and I suspect that I have read one more carriage return, leading to a Type Mismatch exception.
After some attempts, I fixed the problem by changing the parameter num in the Code's string S = new string (B, 0, num) to num-2.
But this also raises a problem: Does the carriage return occupy two bytes?