Home > Java > javaTutorial > Detailed explanation of the implementation of input data in Java

Detailed explanation of the implementation of input data in Java

黄舟
Release: 2017-08-13 09:46:51
Original
1232 people have browsed it

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, or cin() provided by C++ to obtain keyboard input values. Ready-made functions! Java does not provide such a function, which does not mean that we are helpless when encountering this situation. Please look at the following three solutions:

Several methods will be listed below:

Method 1 : Receive a character from the console and then 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); 
}
Copy after login

Although this method achieves obtaining the input characters from the keyboard, System.out.read() can only obtain one character. At the same time, The type of the incoming variable 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); 
}
Copy after login

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); 
}
Copy after login

This code has shown that the Scanner class regardless of Whether it is a string, integer data or float type variable, you only need to make a small change to achieve the function! Undoubtedly he is the most powerful!

However, there is one thing you need to pay attention to when using the third input method, which is the nextLine() function. There is a function with the same function as his next in the io package. () function, their functions are the same, but what is the difference in implementation? Please look at 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); 
}
Copy after login

The difference between this code and the example code given above for the third implementation input method is that this The code first executes nextInit() and then nextLine(). An example of the third method is to execute nextLine() first and then nextInit(). When you are running the two pieces of code, you will find the third method. The example can achieve normal input, 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 does not mean that it is not executed. nextLine function), I mentioned the function next() which has the same function as nextLine(). The difference between them is that the next() function does not receive carriage returns, tabs, or space keys, etc., so use the nextLine() function When typing, be aware that the carriage return character you hit may be absorbed by it, causing a BUG in the program! ! !

Finally, let’s briefly summarize the difference between next() and nextLine():

In java, the next() method does not accept spaces. Before receiving valid data, all Inputs such as spaces or tab keys are ignored. If there is valid data, these keys are encountered and exited. nextLine() can accept spaces or tab keys, and its input should end with the enter key.

The above is the detailed content of Detailed explanation of the implementation of input data in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template