Java Console Input Scanner

Source: Internet
Author: User

There are two common ways to get console input (system.in) in 1,java: (1) BufferedReader  scin=new BufferedReader (new InputStreamReader (system.in)); (2) Java.util.Scanner scin=new Jave.util.Scanner (system.in); 2, using Scanner Method: (1) whether there is input: Hasnext () ====> The method is the terminal has been waiting for the console input state, if you do not control, it is always in the console input state; (2) Gets the input data: (String) Next (), (INT) nextnt ()  and Hasnextint (), (Long) Nextlong ()  hasnextlong (), ... ; Pay attention to logical judgment, because Java also has the same stealth type conversion (Low type auto-convert high type) even if to get int and double, first to determine whether it is hasnextint, in judging hasnextdouble;3, use instance:

Class testscanner{public void input () {Scanner sc=new Scanner (system.in); String exit= "Exit"; System.out.println ("Enter data: (exit indicates exit instruction)"), while (Sc.hasnext ()) {if (Sc.hasnextbyte ()) {System.out.println ("Enter a byte: "+sc.nextbyte ());} else if (Sc.hasnextint ()) {System.out.println ("entered an integer:" +sc.nextint ());} else if (sc.hasnextdouble ()) {System.out.println ("entered a floating-point number:" +sc.nextdouble ());} else if (Sc.hasnextboolean ()) {System.out.println ("entered a Boolean value:" +sc.nextboolean ());} else if (Sc.next (). Contentequals (Exit)) {SYSTEM.OUT.PRINTLN ("Exit! "); break;}}}
4, Results analysis:
Please enter data: (exit indicates exit instruction) 123 876 9.0 true false exit enters a byte: 123 enters an integer: 876 enters a floating-point number: 9.0 enters a Boolean value: True enters a Boolean value: false exits!

</pre><p></p><p></p><p></p><pre name= "code" class= "Java" >

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Console Input Scanner

Related Article

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.