How to get keyboard input values in Java

Source: Internet
Author: User

1In the process of program development, the need to get input values from the keyboard is common, but Java it is not like the C language to provide us with scanf (), C + + gives us the CIN () to get the keyboard input value of the ready-made function!Java does not provide such a function does not mean that we do not have the experience of this situation, please see the following three ways to solve it:2 3 Several methods are listed below:4 5 method One: Receive a character from the console and print it out6 7      Public Static voidMain (String [] args)throwsioexception{8 9 System.out.print ("Enter a Char:");Ten  One     Chari = (Char) System.in.read (); A  -System.out.println ("YourCharis: "+i); -  the } -  - } -  + Although this method implements the input character from the keyboard, System.out.read () can only be obtained for one character, and the type of the variable entered is only char, and when we enter a number, we want to get an integer variable, We also have to modify the variable types, which makes it more cumbersome.  -  + method Two: Receive a string from the console and print it out. In this topic, we need to use the BufferedReader class and the InputStreamReader class A  at      Public Static voidMain (String [] args)throwsioexception{ -  -BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); -  -String str =NULL; -  in System.out.println ("Enter Your Value:"); -  tostr =br.readline (); +  -SYSTEM.OUT.PRINTLN ("Your value is:" +str); the  * } $ Panax Notoginseng so we can get the string we entered.  -  the method Three: This method I think is the simplest, the most powerful, is to use the scanner class +  A      Public Static voidmain (String [] args) { the  +Scanner sc =NewScanner (system.in); -  $ System.out.println ("Please enter your name:"); $  -String name =sc.nextline (); -  the System.out.println ("Please enter your Age:"); - Wuyi     intAge =sc.nextint (); the  - System.out.println ("Please enter your salary:"); Wu  -     floatSalary =sc.nextfloat (); About  $ System.out.println ("Your information is as follows:"); -  -System.out.println ("Name:" +name+ "\ n" + "Age:" +age+ "\ n" + "Salary:" +salary); -  A } +  theThis code has shown that the scanner class, whether it's a string or integer data or a variable of type float, can do it with a little bit of change! No doubt he is the strongest!

How to get keyboard input values in Java

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.