Java program get keyboard input (reprint)

Source: Internet
Author: User

Java Program development process, need to get input values from the keyboard is often the case, 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, we see the following three solutions:
method One: Receive a character from the console and print it out

code example:

1     // System.in.read () method to receive only a single character 2     Private Static void throws IOException {3         char ch = (char) System.in.read (); 4         SYSTEM.OUT.PRINTLN ("Keyboard input is:" +ch); 5     }
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

BufferedReader is a character in the Io stream, a wrapper stream, which must be based on another stream of characters. But the standard input system.in is a byte stream, so you need to wrap it into a character stream using InputStreamReader, so this is usually the case in the program:

bufferedreader br = new BufferedReader (new InputStreamReader (system.in));

code example:

1     //BufferedReader class Implementation2     Private Static voidINPUTMETHOD2 ()throwsIOException {3BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));4String buffer =NULL;5          while(buffer = Br.readline ())! =NULL) {6SYSTEM.OUT.PRINTLN ("Keyboard input is:" +buffer);7         }8}
so we can get the string we entered. method Three: This method I think is the simplest, the most powerful, is to use the scanner class

Scanner is a new tool class for JDK1.5, and using the scanner class makes it easy to get keyboard input, a text scanner based on regular expressions that resolves basic type values and string values from files, input streams, and strings.

code example:
1     //Scanner class implementation2     Private Static voidinputMethod3 () {3Scanner sc =NewScanner (system.in);4System.out.println ("Please enter your name:"); 5String name =sc.nextline ();6System.out.println ("Please enter your Age:"); 7         intAge =sc.nextint ();8System.out.println ("Please enter your salary:"); 9         floatSalary =sc.nextfloat ();TenSYSTEM.OUT.PRINTLN ("Your information is as follows:");  OneSystem.out.println ("Name:" +name+ "\ n" + "Age:" +age+ "\ n" + "Salary:" +salary);  A sc.close (); -}
This code has shown that the scanner class, whether it's a string or integer data, or a variable of type float, only needs a little bit of change to be able to implement the function! No doubt he is the most powerful!  however, when using the third input method, there is a need to note that the nextline () function, in the IO package has a function of the same functions as I next () function, they function like, but in the implementation of what difference, see the following code:
1     /*2 * The difference and comparison between the next () method and the Nextint () method in the Scanner class3 * Next () method is not to receive the space, before the receipt of valid data, all the space or TAB key input is ignored, if there is valid data, you encounter these keys to exit. 4 * nextline () can receive a space or TAB key, and its input should end with the ENTER key. 5      */6     Private Static voidInputcont () {7Scanner sc =NewScanner (system.in);8System.out.println ("Please enter your Age:"); 9         intAge =sc.nextint ();TenSystem.out.println ("Please enter your name:");  OneString name =sc.nextline (); ASystem.out.println ("Please enter your salary:");  -         floatSalary =sc.nextfloat (); -SYSTEM.OUT.PRINTLN ("Your information is as follows:");  theSystem.out.println ("Name:" +name+ "\ n" + "Age:" +age+ "\ n" + "Salary:" +salary);  - sc.close (); -}
This code differs from the example code given in the third implementation of the input method above, in which the code executes nextinit () and then executes Nextline (), while the third method is performed nextline () first, then Nextinit (), When you are running two pieces of code, you will find the third method of the example can achieve normal input, and this code is entered age, after hitting enter key, skip the input name, directly to the input salary here, (you can run the code to see) This is why? In fact, after executing the nextinit () function, hitting the Enter enter key, the carriage return will be absorbed by the nextline () function, which actually executes the nextline () function to absorb the input carriage return (not without executing the nextline function), mentioned earlier and Nextline () functions the same function next (), their difference is that: next () function will not receive carriage return and tab, or space bar, etc., so in the use of nextline () function, pay attention to the strike of the carriage return is absorbed by it, causing the program to appear bug!!!   Finally, a small summary of the difference between next () and nextline () :

In Java, the next () method is not to receive the space, before the receipt of valid data, all the space or TAB key input is ignored, if there is valid data, you encounter these keys exit. nextline () can receive a space or TAB key, and its input should end with the ENTER key.

Full-text reference from: https://www.cnblogs.com/elice/p/5662227.html

Java program get keyboard input (reprint)

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.