首页 > Java > java教程 > 正文

Java 扫描器类

WBOY
发布: 2024-08-30 16:10:08
原创
391 人浏览过

在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:

Java 扫描器类

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:

Java 扫描器类

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板