User interaction Scanner

User interaction Scanner

Java provides us with such a tool class that we can get the user's input. java.util.Scanner is a new feature of Java 5. You can get the user's input through the scanner class.

Basic syntax:
    Scanner s = new Scanner(System.in);

Get the input string through the next() and nextLine() methods of the Scanner class. Before reading, we usually need to use hasNext() and hasNextLine() to determine whether there is input data.

  • Use next() and hasNext()
package com.wang.scanner;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //Create a scanner object to receive user input
        Scanner scanner = new Scanner(System.in);

        System.out.println("Use next Mode reception:");

        //Judge whether the user has input
        if (scanner.hasNext()) {
            //Used to receive user input
            String str = scanner.next();
            System.out.println("The input content is:" + str);
        }

        /*If a class belonging to IO stream is not closed, it will always occupy resources
        So close the resource after use*/
        scanner.close();
    }
} 
  • Use nextLine() and hasNextLine()
package com.wang.scanner;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        //Create a scanner object to receive user input
        Scanner scanner = new Scanner(System.in);

        System.out.println("Use nextLine Received by:");

        //Judge whether the user has input
        if (scanner.hasNextLine()) {
            //Used to receive user input
            String s = scanner.nextLine();
            System.out.println("The input content is:" + s);
        }

        //close resource
        scanner.close();
    }
}
next():
	1.You must read valid characters before you can end the input.
    2.For blanks encountered before entering valid characters,next()Method automatically removes it.
    3.The space entered after a valid character is used as a separator or terminator only after it is entered.
    4.next()Cannot get a string with spaces.
     
nextLine():
	1.with Enter For the closing sign, that is,nextLine()Method returns all the characters before enter.
    2.You can get blank.
  • Don't use judgment statements
package com.wang.scanner;

import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        //Create a scanner object to receive user input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter:");

        //Used to receive user input
        String s = scanner.nextLine();
        System.out.println("The input content is:" + s);

        //close resource
        scanner.close();
    }
}
  • When you enter integer or floating-point data of these types
package com.wang.scanner;

import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        //Create a scanner object to receive user input
        Scanner scanner = new Scanner(System.in);

        //Receive data from keyboard
        int i = 0;
        float f = 0.0F;

        System.out.println("Please enter an integer:");
        //Judge whether the input is an integer
        if (scanner.hasNextInt()) {
            i = scanner.nextInt();
            System.out.println("The integer entered is:" + i);
        } else {
            System.out.println("The input is not an integer!");
        }

        System.out.println("Please enter decimal:");
        //Determine whether the input is a decimal
        if (scanner.hasNextFloat()) {
            f = scanner.nextFloat();
            System.out.println("The decimal entered is:" + f);
        } else {
            System.out.println("The input is not a decimal!");
        }
        scanner.close();
    }
}

**Case: * * input multiple numbers and calculate their sum and average. For each number input, use enter to confirm. Input non number to end the input and output the execution result

package com.wang.scanner;

import java.util.Scanner;

public class Demo7 {
    /*
    * Input multiple numbers and find the sum and average of them. Input one number for each number
    * Confirm with enter key, end the input and output the execution result by inputting non number
    * */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //and
        double sum = 0;
        //Calculate how many numbers have been entered
        int m = 0;

        System.out.println("Please enter a number!");
        //Judge whether there is any input through circulation, and sum each time in it
        while (scanner.hasNextDouble()) {
            double x = scanner.nextDouble();
            m = m + 1;
            sum = sum + x;
            System.out.println("You entered the" + m + "Data, current total is sum=" + sum +
                    "The average is:" + (sum / m));
        }
        System.out.println(m + "The sum of the numbers is:" + sum);
        System.out.println(m + "The average number is:" + (sum / m));

        scanner.close();
    }
}
Published 8 original articles, won praise 2, visited 118
Private letter follow

Tags: Java

Posted on Sun, 02 Feb 2020 10:56:33 -0500 by Reef