Java learning note

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.