The java program obtains the keyboard input, and the java program obtains the keyboard input.

Source: Internet
Author: User

The java program obtains the keyboard input, and the java program obtains the keyboard input.
During Java program development, it is common to obtain the input value from the keyboard, but Java does not provide scanf () for us like the C language (), C ++ provides us with a ready-made function to obtain the input value of the keyboard! If Java does not provide such a function, it does not mean that we are helpless in this case. Let's look at the following three solutions:
Method 1: receive a character from the console and print it out.

Sample Code:

1 // System. in. read () method, can only receive a single character 2 private static void inputMethod1 () throws IOException {3 char ch = (char) System. in. read (); 4 System. out. println ("keyboard input:" + ch); 5}
Although this method obtains the input characters from the keyboard. out. read () can only be obtained for one character. At the same time, the retrieved variable type can only be char. When we enter a number and want to obtain an integer variable, we have to modify the variable type, which makes it quite troublesome. Method 2: receive a string from the console and print it out. In this question, we need to use the BufferedReader class and InputStreamReader class.

BufferedReader is a character and packaging stream in the io stream. It must be built on another worker stream. However, the standard input System. in is a byte stream, so you need to use InputStreamReader to package it into a bytes stream. Therefore, the program usually writes the following:

BufferedReader br = new BufferedReader (new InputStreamReader (System. in ));

Sample Code:

1 // BufferedReader class implements 2 private static void inputMethod2 () throws IOException {3 BufferedReader br = new BufferedReader (new InputStreamReader (System. in); 4 String buffer = null; 5 while (buffer = br. readLine ())! = Null) {6 System. out. println ("keyboard input:" + buffer); 7} 8}
In this way, we can obtain the input string. Method 3: I think this method is the simplest and most powerful, that is, the use of the struct class.

The Scanner is a new tool class in JDK. It can be used to conveniently obtain keyboard input. It is a text Scanner Based on regular expressions, you can parse basic type values and string values from files, input streams, and strings.

Sample Code:
1 // implement 2 private static void inputMethod3 () {3 then SC = new then (System. in); 4 System. out. println ("enter your name:"); 5 String name = SC. nextLine (); 6 System. out. println ("Enter your age:"); 7 int age = SC. nextInt (); 8 System. out. println ("Enter your salary:"); 9 float salary = SC. nextFloat (); 10 System. out. println ("your information is as follows:"); 11 System. out. println ("name:" + name + "\ n" + "age:" + age + "\ n" + "salary:" + salary); 12 SC. close (); 13}
This Code already shows that the functions can be implemented by making a slight change to variables of the string, integer, or float type! No doubt he is the most powerful! However, when using the third input method, you need to pay attention to the nextLine () function. In the io package, there is a function that has the same function as other functions. My next () function, their functions are the same, but what are the differences in implementation? Please refer to the following code:
1/* 2 * differences and comparisons between the next () method and nextInt () method in the values Class 3 * the next () method does not receive spaces, all spaces, tab keys, and other inputs are ignored before valid data is received. If valid data exists, these keys exit. 4 * nextLine () can receive spaces or tab keys. The input should end with the enter key. 5 */6 private static void inputCont () {7 then SC = new then (System. in); 8 System. out. println ("Enter your age:"); 9 int age = SC. nextInt (); 10 System. out. println ("enter your name:"); 11 String name = SC. nextLine (); 12 System. out. println ("Enter your salary:"); 13 float salary = SC. nextFloat (); 14 System. out. println ("your information is as follows:"); 15 System. out. println ("name:" + name + "\ n" + "age:" + age + "\ n" + "salary:" + salary); 16 SC. close (); 17}
The difference between this code and the example code provided by the third input method is that the Code runs nextInit () before nextLine (), the example of the third method is to first execute nextLine (), then execute nextInit (), when you are running two sections of code, you will find that the example of the third method can achieve normal input, but this section of code is entering the age, after hitting the enter key, after skipping the input name, you can directly enter the salary here (you can run the code to see). Why? In fact, after the nextInit () function is executed, the enter key is hit, and the carriage return is absorbed by the nextLine () function. In fact, the nextLine () function is executed () the function absorbs the input carriage return (not the nextLine function is not executed). The function next (), which has the same function as nextLine (), has the following differences: next () the function does not receive carriage returns, tabs, or space keys. Therefore, when using the nextLine () function, you must note that a carriage return is absorbed by it, causing a BUG in the program !!! Finally, let's briefly summarize the differences between next () and nextLine ().:

In java, the next () method does not receive spaces. Before receiving valid data, all spaces, tab keys, and other inputs are ignored. If valid data exists, these keys exit. NextLine () can receive spaces or tab keys. Its input should end with the enter key.

 

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

 

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.