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