[Java study notes] the use of Scanner class

1. Scanner class overview

After JDK5 is used to obtain the user's keyboard input

2. The construction method of Scanner class

  • public Scanner (InputStream source): Construct a new Scanner, the value it generates is scanned from the specified input stream.

For specific usage, please refer to the following code:

package com.hw.scanner;

import java.util.Scanner;

/**
 * Scanner类概述
 * JDK5以后用于获取用户的键盘输入
 * 
 * Scanner类的构造方法
 * public Scanner(InputStream source):构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。
 * 
 * System类下有一个静态的字段
 * 		public static final InputStream in;标准的输入流,对应着键盘录入。
 * 		InputStream input = System.in;
 * 
 * @author HW
 * 
 */
public class ScannerDemo {
	public static void main(String[] args) {
		// 创建Scanner对象
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个整数:");
		int x = sc.nextInt();
		System.out.println("x=" + x);
	}
}

The results of the operation are as follows: 

请输入一个整数:10
x=10

 3. Common member methods of Scanner class

The basic format is as follows:

  • public boolean hasNextXxx(): Determine whether it is a certain type of element, where Xxx can be int, double, etc. If you need to determine whether to include the next string, you can omit Xxx
  • public Xxx nextXxx(): Get the element

Take the int type as an example:

  • public boolean hasNextInt(): Determine whether there is an element of type int
  • public int nextInt(): Get an int type value 
  • public String nextLine(): Get a String type value

The specific usage of hasNextInt() refers to the following code: 

package com.hw.scanner;

import java.util.Scanner;

/**
 * @author HW
 *
 */
public class ScannerDemo2 {
	public static void main(String[] args) {
		// 创建Scanner对象
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个整数:");

		if (sc.hasNextInt()) {
			int x = sc.nextInt();
			System.out.println("你输入的整数为:" + x);
		} else {
			System.out.println("你输入的数据不是整数!");
		}
	}
}

The results of the operation are as follows:

请输入一个整数:10
你输入的整数为:10

Let's look at a requirement: get the value of int type first, and then get the value of String type, the code is as follows:

/**
 * 先获取int类型的值,再获取String类型的值
 */
private static void method4() {
	// 创建Scanner对象
	Scanner sc = new Scanner(System.in);

	// 先获取int类型的值,再获取String类型的值
	System.out.print("请输入一个整数:");
	int x = sc.nextInt();

	System.out.print("请输入一个字符串:");
	String str = sc.nextLine();

	System.out.println("x=" + x + " str=" + str);
	}

There will be problems, as follows:

请输入一个整数:10
请输入一个字符串:x=10 str=

The main reason: the problem with the line break

Solution:

  • The first way: get a value first, and then create a new keyboard entry object to get the string.
  • The second way: get all the data according to the string first, and then what you want depends on the corresponding conversion.

code show as below:

package com.hw.scanner;

import java.util.Scanner;

/**
 * Scanner类的常用方法 
 * public int nextInt():获取一个int类型的值 
 * public String nextLine():获取一个String类型的值
 * 
 * @author HW
 * 
 */
public class ScannerDemo3 {
	public static void main(String[] args) {
		// method1();
		// method2();
		// method3();
		method4();
	}

	/**
	 * 先获取int类型的值,再获取String类型的值
	 * 
	 * 会出现问题,如下:
	 * 请输入一个整数:10
	 * 请输入一个字符串:
	 * x=10 str=
	 * 
	 * 主要原因:换行符号的问题
	 * 
	 * 解决方法:
	 * A:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
	 * B:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。
	 */
	private static void method4() {
		// 创建Scanner对象
		Scanner sc = new Scanner(System.in);

		// 先获取int类型的值,再获取String类型的值
		System.out.print("请输入一个整数:");
		int x = sc.nextInt();

		System.out.print("请输入一个字符串:");
		// String str = sc.nextLine();
		
		// 解决方法:A:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
		Scanner sc1 = new Scanner(System.in);
		String str = sc1.nextLine();

		System.out.println("x=" + x + " str=" + str);
	}

	/**
	 * 先获取String类型的值,再获取int类型的值
	 */
	private static void method3() {
		// 创建Scanner对象
		Scanner sc = new Scanner(System.in);

		// 先获取String类型的值,再获取int类型的值
		System.out.print("请输入一个字符串:");
		String str = sc.nextLine();

		System.out.print("请输入一个整数:");
		int x = sc.nextInt();

		System.out.println("str=" + str + " x=" + x);

		System.out.println("========================");
	}
	
	/**
	 * 获取两个String类型的值
	 */
	private static void method2() {
		// 创建Scanner对象
		Scanner sc = new Scanner(System.in);

		// 获取两个String类型的值
		System.out.print("请输入第一个字符串:");
		String str = sc.nextLine();

		System.out.print("请输入第二个字符串:");
		String str1 = sc.nextLine();
		System.out.println("str=" + str + " str1=" + str1);
	}

	/**
	 * 获取两个int类型的值
	 */
	private static void method1() {
		// 创建Scanner对象
		Scanner sc = new Scanner(System.in);

		// 获取两个int类型的值
		System.out.print("请输入第一个整数:");
		int a = sc.nextInt();

		System.out.print("请输入第二个整数:");
		int b = sc.nextInt();
		System.out.println("a=" + a + " b=" + b);
	}
}

 

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/105578495