In the process of Java program development, it is common to need to obtain input values from the keyboard, but Java does not provide us with scanf() like the C language. C provides us with The provided cin() function is a ready-made function to obtain the keyboard input value! Java does not provide such a function, which does not mean that we are helpless when encountering this situation. (Recommended: java video tutorial)
Please take a look at the following three solutions:
Several methods will be listed below:
Methods One: Receive a character from the console and print it out
import java.io.*; public static void main(String [] args) throws IOException{ System.out.print("Enter a Char:"); char i = (char) System.in.read(); System.out.println("your char is :"+i); }
Although this method achieves obtaining the input characters from the keyboard, System.out.read() can only obtain one character, and at the same time , the type of the variable obtained can only be char. When we input a number and hope to get an integer variable, we have to modify the variable type, which is more troublesome.
Method 2: Receive a string from the console and print it out. In this question, we need to use the BufferedReader class and the InputStreamReader class
import java.io.*; public static void main(String [] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; System.out.println("Enter your value:"); str = br.readLine(); System.out.println("your value is :"+str); }
so that we can get the string we input.
Method 3: I think this method is the simplest and most powerful, which is to use the Scanner class
import java.util.Scanner; public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的姓名:"); String name = sc.nextLine(); System.out.println("请输入你的年龄:"); int age = sc.nextInt(); System.out.println("请输入你的工资:"); float salary = sc.nextFloat(); System.out.println("你的信息如下:"); System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary); }
This code has shown that the Scanner class is used for strings or integers. Variables of data or float type can function with just a few small changes! Undoubtedly he is the most powerful!
However, there is one thing that needs to be paid attention to when using the third input method, which is the nextLine() function. There is a function with the same function as him in the io package, my next() function. The functions are the same, but what are the differences in implementation? Please see the following code:
public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的年龄:"); int age = sc.nextInt(); System.out.println("请输入你的姓名:"); String name = sc.nextLine(); System.out.println("请输入你的工资:"); float salary = sc.nextFloat(); System.out.println("你的信息如下:"); System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary); }
The difference between this code and the example code given above for the third implementation input method is that this code first executes nextInit( ) and then execute nextLine(), and the example of the third method is to execute nextLine() first, and then execute nextInit(). When you are running the two pieces of code, you will find that the example of the third method can achieve normal Enter, but this code is inputting age. After hitting the enter key, it skips entering the name and goes directly to entering the salary. (You can run the code yourself to see) Why is this?
In fact, after executing the nextInit() function and pressing the enter key, the carriage return character will be absorbed by the nextLine() function. In fact, the nextLine() function is executed to absorb the entered carriage return. character (it is not that the nextLine function is not executed). As mentioned earlier, the function next() has the same function as nextLine(). The difference between them is that the next() function does not receive the carriage return character, tab, or space bar, etc., so When using the nextLine() function, be aware that the carriage return character you type may be absorbed by it, causing a BUG in the program! ! !
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of How to get input in java?. For more information, please follow other related articles on the PHP Chinese website!