Small white school recruit machine entrance into the pit the various data types of Java input from the keyboard

Source: Internet
Author: User

//1. Enter an integral type from the keyboard (similar to other basic types)Scanner sc =NewScanner (system.in); Sc.hasnextint ();intSTR1 =sc.nextint ();//2. Enter a fixed-length integer array from the keyboardint[] arr =New int[21]; Scanner SC=NewScanner (system.in);  for(inti=0;i<21;i++) {sc.hasnextint (); Arr[i]=sc.nextint ();}//3. Enter a string from the keyboard (no spaces)Scanner sc =NewScanner (system.in); Sc.hasnext (); String str1=Sc.next ();//4. Enter a line of string from the keyboard (can contain spaces)Scanner sc =NewScanner (system.in); Sc.hasnextline (); String str1=sc.nextline ();//5. Enter a comma-separated array of integers from the keyboard, with a negative integer and an indefinite length//  such as 1,2,-3,4,-5,6 ...
Note Integer.parseint cannot parse negative integertype of string, really pit, at that time this small white exam last together defeated on this, a bitter tearScanner sc =NewScanner (system.in); Sc.hasnextline (); string[] Str= Sc.nextline (). Split (","); int[] arr =New int[Str.length]; for(inti=0;i<str.length;i++){ if(Str[i].startswith ("-") ) {Arr[i]=-integer.parseint (str[i].substring (1)); }Else{Arr[i]=Integer.parseint (Str[i]); } }

is a pit stepping over personal experience, Xiao Xu hope to help the lucky little white people.

Xiao Xu See the world, the world so beauty: http://www.cnblogs.com/schoolbag/

Small white school recruit machine entrance into the pit the various data types of Java input from the keyboard

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.