在Java中,Scanner是一个类,用于获取字符串和不同原始类型(例如int、double等)的输入。scanner类位于java.util.concurrent包中。它扩展了 Object 类并实现了 Closeable 和 Iterator 接口。在空格分隔符的帮助下,输入被分成类。 Java 中 Scanner 类的声明、语法、工作和其他几个方面将在以下部分中讨论。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Java 扫描器类声明
Java Scanner 类声明如下。
public final class Scanner extends Object implements Iterator<string></string>
语法:
Scanner 类可以在 Java 程序中按以下语法使用。
Scanner sc = new Scanner(System.in);
这里,sc 是扫描仪对象,System.in 告诉 Java 输入将是这个。
Java Scanner 类如何工作?
现在,让我们看看 Scanner 类是如何工作的。
- 导入类 java.util.Scanner 或整个包 java.util。
import java.util.*; import java.util.Scanner;
- 按照语法所示创建扫描仪对象。
Scanner s = new Scanner(System.in);
- 声明 int、char 或 string 类型。
int n = sc.nextInt();
- 根据需求对输入进行操作。
构造函数
Java中的扫描器类有不同的构造函数。他们是:
- 扫描仪(文件源): 将构造一个新的扫描仪,从上述文件生成值。
- Scanner(File src, String charsetName): 将构造一个新的扫描器,生成从上述文件扫描的值。
- Scanner(InputStream src): 将构造一个新的扫描器,从上述输入流生成值。
- Scanner(InputStream src, String charsetName): 将构造一个新的扫描器,从上述输入流生成值。
- 扫描仪(路径 src): 将构建一个新的扫描仪,从上述文件生成值。
- Scanner(Path src, String charsetName): 将构造一个新的扫描器,从上述文件生成值。
- 扫描仪(可读源): 将构建一个新的扫描仪,从上述源生成值。
- Scanner(ReadableByteChannel src): 将构建一个新的扫描器,从上述通道生成值。
- Scanner(ReadableByteChannel src, String charsetName): 将构造一个新的扫描器,从上述通道生成值。
- Scanner(String src): 将构造一个新的扫描器,从上述字符串生成值。
Java Scanner 类的方法
以下是执行不同功能的方法。
- close(): Scanner gets closed on calling this method.
- findInLine(Pattern p): Next occurrence of the mentioned pattern p will be attempted to find out, ignoring the delimiters.
- findInLine(String p): The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
- delimiter(): Pattern that is currently used by the scanner to match delimiters will be returned.
- findInLine(String p): The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
- findWithinHorizon(Pattern p, int horizon): Tries to identify the mentioned pattern’s next occurrence.
- hasNext(): If the scanner has another token in the input, true will be returned.
- findWithinHorizon(String p, int horizon): Tries to identify the next occurrence of the mentioned pattern made from the string p, ignoring delimiters.
- hasNext(Pattern p): If the next token matches the mentioned pattern p, true will be returned.
- hasNext(String p): If the next token matches the mentioned pattern p made from the string p, true will be returned.
- next(Pattern p): Next token will be returned, which matches the mentioned pattern p.
- next(String p): Next token will be returned, which matches the mentioned pattern p made from the string p.
- nextBigDecimal(): Input’s next token will be scanned as a BigDecimal.
- nextBigInteger(): Input’s next token will be scanned as a BigInteger.
- nextBigInteger(int rad): Input’s next token will be scanned as a BigInteger.
- nextBoolean(): Input’s next token will be scanned as a Boolean, and it will be returned.
- nextByte(): Input’s next token will be scanned as a byte.
- nextByte(int rad): Input’s next token will be scanned as a byte.
- nextDouble(): Input’s next token will be scanned as a double.
- nextFloat(): Input’s next token will be scanned as a float.
- nextInt(): Input’s next token will be scanned as an integer.
- nextInt(int rad): Input’s next token will be scanned as an integer.
- nextLong(int rad): Input’s next token will be scanned as a long.
- nextLong(): Input’s next token will be scanned as a long.
- nextShort(int rad): Input’s next token will be scanned as a short.
- nextShort(): Input’s next token will be scanned as a short.
- radix(): The default radix of the scanner will be returned.
- useDelimiter(Pattern p): Delimiting pattern of the scanner will be set to the mentioned pattern.
- useDelimiter(String p): Delimiting pattern of the scanner will be set to the mentioned pattern made from the string p.
- useRadix(int rad): The scanner’s default radix will be set to the mentioned radix.
- useLocale(Locale loc): The locale of the scanner will be set to the mentioned locale.
Examples of Java Scanner Class
Now, let us see some sample programs of the Scanner class in Java.
Example #1
Code:
import java.util.Scanner; class ScannerExample { public static void main(String[] args) { //create object of scanner Scanner sc = new Scanner(System.in); System.out.println("Enter the username"); String obj = sc.nextLine(); System.out.println("The name you entered is: " + obj); } }
Output:
On executing the program, the user will be asked to enter a name. As the input is a string, the nextLine() method will be used. Once it is entered, a line will be printed displaying the name user given as input.
Example #2
Code:
import java.util.Scanner; class ScannerExample { public static void main(String[] args) { //create object of scanner Scanner sc = new Scanner(System.in); System.out.println("Enter age"); int obj = sc.nextInt(); System.out.println("The age you entered is: " + obj); } }
Output:
On executing the program, the user will be asked to enter the age. As the input is a number, the nextInt() method will be used. Once it is entered, a line will be printed displaying the age user given as input.
以上是Java 扫描器类的详细内容。更多信息请关注PHP中文网其他相关文章!

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

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

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

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

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

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

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

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


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)