Detailed explanation of java scanner usage
Java Scanner class
java .util.Scanner is a new feature of Java5. We can obtain user input through the Scanner class.
The following is the basic syntax for creating a Scanner object:
Scanner s = new Scanner(System.in);
Next we demonstrate the simplest data input and obtain the input characters through the next() and nextLine() methods of the Scanner class String, before reading, we generally need to use hasNext and hasNextLine to determine whether there is still input data:
Use next method:
ScannerDemo.java file code:
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // next方式接收字符串 System.out.println("next方式接收:"); // 判断是否还有输入 if (scan.hasNext()) { String str1 = scan.next(); System.out.println("输入的数据为:" + str1); } scan.close(); } }
The output result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo
next mode reception:
runoob com
The input data is: runoob
You can see that the com string is not output, next we See nextLine.
Use nextLine method:
ScannerDemo.java file code:
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // nextLine方式接收字符串 System.out.println("nextLine方式接收:"); // 判断是否还有输入 if (scan.hasNextLine()) { String str2 = scan.nextLine(); System.out.println("输入的数据为:" + str2); } scan.close(); } }
The output result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo
nextLine mode reception:
runoob com
The input data is: runoob com
You can see the com string output.
The difference between next() and nextLine()
next():
1. The input must be completed only after valid characters are read. .
2. The next() method will automatically remove the blanks encountered before entering valid characters.
3. Only after entering valid characters, use the blank space entered after them as the separator or terminator.
next() cannot get a string with spaces.
nextLine():
1. Enter is the end character, which means that the nextLine() method returns all the characters before entering.
2. Can get blank space.
If you want to input int or float type data, it is also supported in the Scanner class, but it is best to use the hasNextXxx() method to verify before inputting, and then use nextXxx() to read:
ScannerDemo.java file code:
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 int i = 0; float f = 0.0f; System.out.print("输入整数:"); if (scan.hasNextInt()) { // 判断输入的是否是整数 i = scan.nextInt(); // 接收整数 System.out.println("整数数据:" + i); } else { // 输入错误的信息 System.out.println("输入的不是整数!"); } System.out.print("输入小数:"); if (scan.hasNextFloat()) { // 判断输入的是否是小数 f = scan.nextFloat(); // 接收小数 System.out.println("小数数据:" + f); } else { // 输入错误的信息 System.out.println("输入的不是小数!"); } scan.close(); } }
The output result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo 输入整数:12 整数数据:12 输入小数:1.2 小数数据:1.2
In the following example, we can enter multiple numbers and find their sum and average. Each time one is entered Use Enter to confirm the number. End the input by entering a non-number and output the execution result:
ScannerDemo.java file code:
import java.util.Scanner; class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double sum = 0; int m = 0; while (scan.hasNextDouble()) { double x = scan.nextDouble(); m = m + 1; sum = sum + x; } System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值是" + (sum / m)); scan.close(); } }
The output result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo 12 23 15 21.4 end 4个数的和为71.4 4个数的平均值是17.85
Recommendation: "javalearning"
The above is the detailed content of Detailed explanation of java scanner usage. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
