Java 扫描器类
在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>
语法:
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中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

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

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
