Scanner keyboard entry

overview

>(green!)

At present, when writing Java programs, the data is hard-coded and fixed. Sometimes in order to make the program more flexible, it is necessary to flexibly input data.

The function of Scanner is to flexibly receive data input from the keyboard.

But the data in the actual development is more from the front-end page (user input, upload, etc.), so in general Scanner does not have much practical effect on web development, just understand it .

But in the learning stage of JavaSE, we don't have front-end and various interface input information. In the case of a stand-alone computer, using Scanner keyboard input is a very good choice, which can improve the flexibility of the program.

And in some test scenarios, Scanner keyboard input also has certain uses, so it is necessary to learn about Scanner.

Steps for usage

>(green!)

So how should Scanner keyboard input be used?

It can be roughly divided into three steps:

  1. Import package (IDEA has automatic package import function, but don't think that there is no such step)

    import java.util.Scanner;
    
  2. create object

    Scanner sc = new Scanner(System.in);
    
  3. Receive data entered from the keyboard

    int x = sc.nextInt();
    

Precautions:

  1. At this stage, just remember that the steps will be used. Let's analyze what each step is doing in detail later.

  2. Generally, for the convenience of use, an output statement prompting keyboard input should be added.

    Refer to the following code examples:

    keyboard input int data
    Scanner sc = new Scanner(System.in);
    // 键盘接收int数据
    System.out.println("请输入一个int数据:");
    int num = sc.nextInt();
    
  3. When using Scanner to enter data separately, you can choose different methods, for example:

    1. Keyboard input String character string, you can choose to use the nextLine method.
    2. Keyboard input int value, you can choose to use the nextInt method.
    3. To enter a double value with the keyboard, you can choose to use the nextDouble method.
  4. When using Scanner to input multiple data types, it is recommended not to mix the above methods. for example:

    Use different methods of Scanner to input multiple data
    int num = sc.nextInt();
    String str = sc.nextLine();
    

    This way of writing is actually wrong!

    It is recommended to always use the nextLine method when there is a need to enter multiple types of data, and then convert the string into various required data types!

    Refer to the following code:

    Use the nextLine method uniformly
    Scanner sc = new Scanner(System.in);
    System.out.println("请键盘录入一个int整数:");
    String numStr = sc.nextLine();
    // 百度查找将String转换成int的方法
    int num = Integer.parseInt(numStr);
    System.out.println("请键盘录入一个字符串:");
    String str = sc.nextLine();
    System.out.println(num);
    System.out.println(str);
    

    Of course, if you have doubts about the above usage, you can continue to read and learn the reasons.

    But Scanner itself is a person who understands the content, it doesn't matter whether you know the reason or not!

reason

>(green!)

The use of Scanner, the difference between a series of methods such as next(), nextInt() and nextLine():

  1. A series of methods such as next() and nextInt()
    • A series of methods such as next() start scanning when they encounter the first valid character (non-space, non-newline, non-tab)
    • When the first delimiter or terminator (space, newline or tab) is encountered, the scan ends and the scanned content is obtained
    • That is, get the first scanned single string without spaces and line breaks
  2. nextLine() method
    • As can be seen from the method name, this method is to get the content of a line and receive it as a string
    • This method does not end the scan due to spaces or tabs
    • will only end the scan due to a carriage return (line feed)

>(red!)

Due to the characteristic that the nextLine() method ends the scan when it encounters a newline, the following problems arise when using it:

When using the Scanner to receive a numeric type and then use the nextLine() method to receive a string, for example, the following code:

Mixing two Scanner methods
Scanner sc = new Scanner(System.in);
sc.nextInt();
sc.nextLine();

After entering the value and pressing Enter, the program will not wait for and receive a string, but directly end the keyboard entry. The reason is that the nextLine() method ends scanning when it encounters a carriage return, so what the method receives at this time is actually an (absolutely) empty string.

There are many solutions:

  1. Different Scanner objects can be used to receive, so there must be no conflicts

  2. After receiving int data, add a nextLine that does not receive data to receive carriage return

  3. Use the next() method to receive a string, but it should be noted that this method ends with a delimiter, and it is no longer a line

  4. You can uniformly use strings to receive numerical types, and perform type conversion after receiving. (How to do it, you can Baidu it yourself)

    For example, convert to int type (pass in a string and convert it to int to receive)

    Integer.parseInt()

practise

>(red!)

Three simple practice questions

  1. The keyboard inputs two int data, sums the two int data, and outputs the result.
  2. Enter two values ​​from the keyboard to obtain the maximum value of the two values.
  3. The keyboard enters three data at the same time, which are int type, String type, and double type. What should I do?

>(green!)

The reference code is as follows:

Reference code 1
public static void main(String[] args) {
     
     
 Scanner sc = new Scanner(System.in);
 System.out.print("请输入两个int数值:");
 int num1 = sc.nextInt();
 int num2 = sc.nextInt();
 System.out.println("这两个数值的和是:" + (num1 + num2));
}
Reference code 2
public static void main(String[] args) {
     
     
    Scanner sc = new Scanner(System.in);
    System.out.print("请输入两个int数值:");
    int num1 = sc.nextInt();
    int num2 = sc.nextInt();
    System.out.println("这两个数值的最大值是:" + ((num1 > num2) ? num1 : num2));
}
Reference code 3
public static void main(String[] args) {
     
     
    Scanner sc = new Scanner(System.in);
    // 混合录入数据类型时,统一使用nextLine方法
    System.out.println("请输入一个int整数:");
    String numStr = sc.nextLine();
    int num = Integer.parseInt(numStr);
    System.out.println("请输入一个字符串:");
    String str = sc.nextLine();
    System.out.println("请输入一个double数据:");
    String doubleStr = sc.nextLine();
    double doubleNum = Double.parseDouble(doubleStr);
}

Guess you like

Origin blog.csdn.net/gezongbo/article/details/127127819