Java: realize human-computer interaction through Scanner module

Keywords: Java Back-end

preface

Scanner module is almost everywhere. It establishes a communication channel between people and computers. Scanner means scanner in English. As the name suggests, it connects the data we input into the computer into the program variables we set, so that the variables become meaningful in subsequent operations.

In a word, it is simple human-computer interaction.

There are two different methods for Scanner module to receive data: next method and nextLine method.

next method

import java.util.Scanner;
//Here is the call to the package, which is the same as the call to the library in Python, using import
public class Demo01 {
    public static void main(String[] args) {
        //Receive data from keyboard
        Scanner scan = new Scanner(System.in);
        //Scanner means scanner, System means System, and in means in... You can understand it as creating a new scanner and entering data into the System. This "scan" is to scan your keyboard input values
        System.out.println("May I have your name, please?");
        //This line of print function is to remind you to enter data
        //Receive string in next mode
        if(scan.hasNext()){
            //It can be seen that this is an if conditional statement, which is true by default. If data is really received, perform the following steps
            String str1 = scan.next();
            //The received data is assigned to the string variable str1
            System.out.println(str1+"Hello! I am Java8,This is the first man-machine communication. Welcome to Java This is the first step! wish you success!");
            //Variables are spliced with the set statements, which is also a simple human-computer interaction.
        }
        scan.close();
        //After the scanner method is completed, turn it off, just like turning off the power after the scanner is used, so as to avoid wasting power. This is also the same to prevent excessive resource occupation.
    }
}

In this string of code, please try to enter your name - "Zhang San", and you will find that what is printed is a sentence spliced at the beginning of Zhang San.

Now let's make a little change, enter "Zhang San", and add a space between "Zhang" and "San". You will find that only "Zhang" is output, not "three".

Why? Let's take a look at the nextLine method with this question.

nextLine method

import java.util.Scanner;
//Here is the call to the package, which is the same as the call to the library in Python, using import

public class Demo02 {
    public static void main(String[] args) {
        //Receive data from keyboard
        Scanner scan = new Scanner(System.in);
        //Scanner means scanner, System means System, and in means in... You can understand it as creating a new scanner and entering data into the System. This "scan" is to scan your keyboard input values
        System.out.println("May I have your name, please?");
        //This line of print function is to remind you to enter data

        //Receive string in nextline mode
        if (scan.hasNextLine()){
            //It can be seen that this is an if conditional statement, which is true by default. If data is really received, perform the following steps
            String str2 = scan.nextLine();
            //The received data is assigned to the string variable str2
            System.out.println(str2+"Hello! I am Java8,This is the first man-machine communication. Welcome to Java This is the first step! wish you success!");
            //Variables are spliced with the set statements, which is also a simple human-computer interaction.
        }
        scan.close();
        //After the scanner method is completed, turn it off, just like turning off the power after the scanner is used, so as to avoid wasting power. This is also the same to prevent excessive resource occupation.
    }
}

This time when we input "Zhang San", we will find that even with spaces, it will be completely output!

"Laugh" to see the difference between the next method and the nextLine method

This problem does not need to be too complicated. If a space appears in the middle of the input data using the next method, the data after the space will not be recorded.

The nextLine method is different. It is more powerful and can receive data with spaces. The following is a more detailed analysis.

What if you want to input Int and float

Careful students will find that the data entered in the above two examples are of string type. What should we do if we want to input data of type int and float? In fact, the Scanner module has built-in implementation methods for us, but it is best to use hasNextXxx() method for verification before input, and then use nextXxx() to read.

Read the following code carefully and you will have a clear understanding.

public class Demo04 {
        public static void main(String[] args) {
            System.out.println("Please enter a number:");
            //This line of print function is to remind you to enter data
            Scanner scan = new Scanner(System.in);
            //Scanner means scanner, System means System, and in means in... You can understand it as creating a new scanner and entering data into the System. This "scan" is to scan your keyboard input values
            double sum = 0;
            //Define a double variable
            int m = 0;
            //Define an integer variable
            while (scan.hasNextDouble()){
                //This is a circular statement
                double x = scan.nextDouble();
                m = m+1;
                sum = sum + x;

            }
            System.out.println(m+"The sum of the numbers is:"+sum);
            System.out.println(m+"The average number is"+(sum/m));
            scan.close();
            //After the scanner method is completed, turn it off, just like turning off the power after the scanner is used, so as to avoid wasting power. This is also the same to prevent excessive resource occupation.
        }
    }

Last words

After learning this module, you can actually develop some simple terminal operation tools, such as simple addition, subtraction, multiplication and division, and even complex physical and chemical formulas, which can be realized with the help of Scanner module. After learning the graphical interface in the future, it will become an essential data receiving tool.

Like friends, please give a "one key three links" + "attention" and make progress together!

Posted by Catharsis on Mon, 06 Dec 2021 16:58:57 -0800