在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 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器