Example analysis of the usage of Java Scanner class and the problem of line break caused by nextLine of

  • 2021-07-09 08:11:47
  • OfStack

This paper illustrates the usage of Java Scanner class and the problem of line break caused by nextLine (). Share it for your reference, as follows:

Analysis and understanding: Scanner sc = new Scanner(System.in);


package cn.itcast_01;
/*
 * Scanner: Used to receive data entered by keyboard. 
 *
 *  In front: 
 *     A: Guide pack 
 *     B: Create an object 
 *     C: Invoke method 
 *
 *  Analysis and understanding: Scanner sc = new Scanner(System.in);
 * System Under the class there are 1 Static fields: 
 *     public static final InputStream in;  Standard input stream, corresponding to keyboard entry. 
 *
 *     InputStream is = System.in;
 *
 * class Demo {
 *     public static final int x = 10;
 *     public static final Student s = new Student();
 * }
 * int y = Demo.x;
 * Student s = Demo.s;
 *
 *
 *  Construction method: 
 *     Scanner(InputStream source)
 */
import java.util.Scanner;
public class ScannerDemo {
  public static void main(String[] args) {
    //  Create an object 
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    System.out.println("x:" + x);
  }
}

Class Scanner hasNextInt() And nextInt() Method


package cn.itcast_02;
import java.util.Scanner;
/*
 *  Basic format: 
 *     public boolean hasNextXxx(): Determine whether it is a certain type of element 
 *     public Xxx nextXxx(): Get the element 
 *
 *  Example: Use int Examples of methods of type 
 *     public boolean hasNextInt()
 *     public int nextInt()
 *
 *  Note: 
 *     InputMismatchException What you entered does not match what you want 
 */
public class ScannerDemo {
  public static void main(String[] args) {
    //  Create an object 
    Scanner sc = new Scanner(System.in);
    //  Get data 
    if (sc.hasNextInt()) {
      int x = sc.nextInt();
      System.out.println("x:" + x);
    } else {
      System.out.println(" The data you entered is incorrect ");
    }
  }
}

In the Scanner class nextLine() The resulting newline character problem


package cn.itcast_03;
import java.util.Scanner;
/*
 *  Two commonly used methods: 
 *     public int nextInt(): Get 1 A int Value of type 
 *     public String nextLine(): Get 1 A String Value of type 
 *
 *  There is a problem: 
 *      Get first 1 Numeric value, when getting the 1 Strings, there will be problems. 
 *      The main reason: It is the problem of the line break symbol. 
 *  How to solve it ?
 *     A: Get first 1 After the number of values is created, the 1 A new keyboard entry object gets a string. 
 *     B: Get all the data according to the string first, and then what you want, you will convert it accordingly. 
 */
public class ScannerDemo {
  public static void main(String[] args) {
    //  Create an object 
    Scanner sc = new Scanner(System.in);
    //  Get two int Value of type 
    // int a = sc.nextInt();
    // int b = sc.nextInt();
    // System.out.println("a:" + a + ",b:" + b);
    // System.out.println("-------------------");
    //  Get two String Value of type 
    // String s1 = sc.nextLine();
    // String s2 = sc.nextLine();
    // System.out.println("s1:" + s1 + ",s2:" + s2);
    // System.out.println("-------------------");
    //  Get first 1 String, when getting the 1 A int Value 
    // String s1 = sc.nextLine();
    // int b = sc.nextInt();
    // System.out.println("s1:" + s1 + ",b:" + b);
    // System.out.println("-------------------");
    //  Get first 1 A int Value, when getting the 1 String, there will be a problem here 
    // int a = sc.nextInt();
    // String s2 = sc.nextLine();
    // System.out.println("a:" + a + ",s2:" + s2);
    // System.out.println("-------------------");
    int a = sc.nextInt();
    Scanner sc2 = new Scanner(System.in);
    String s = sc2.nextLine();
    System.out.println("a:" + a + ",s:" + s);
  }
}

For more readers interested in java related content, please check the topics of this site: "Summary of Java File and Directory Operation Skills", "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: