Java Scanner next()方法與示例

掃描儀類的next()方法 (Scanner Class next() method)

Syntax:

句法:

    public String next();
public String next(Pattern patt);
public String next(String patt);

  • next() method is available in java.util package.

    next()方法在java.util包中可用。

  • next() method is used to search & get the next complete token from this Scanner and a token is preceded & followed by the input that meets the pattern.

    next()方法用于搜索并從此掃描器中獲取下一個完整令牌,令牌之前和之后是符合模式的輸入。

  • next(Pattern patt) method is used to retrieve the next token when it meets the given Pattern (patt).

    next(Pattern patt)方法用于在遇到給定Pattern(patt)時檢索下一個標記。

  • next(String patt) method is used to retrieve the next token when it meets the pattern formed from the given String (patt).

    next(String patt)方法用于在下一個令牌遇到由給定String(patt)形成的模式時檢索下一個令牌。

  • These methods may throw an exception at the time of representing a token as a pattern.

    這些方法在將令牌表示為模式時可能會引發異常。

    • NoSuchElementException: This exception may throw when no more token exists.NoSuchElementException :如果不再存在令牌,則可能引發此異常。
    • IllegalStateException: This exception may throw when this Scanner is not opened.IllegalStateException :如果未打開此掃描器,則可能引發此異常。
  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.

    這些是非靜態方法,可通過類對象訪問;如果嘗試使用類名稱訪問這些方法,則會收到錯誤消息。

Parameter(s):

參數:

  • In the first case, next(),

    在第一種情況下, next()

    • It does not accept any parameter.
  • In the first case, next(Pattern patt),

    在第一種情況下, next(Pattern patt)

    • Pattern patt – represents the pattern (patt) to read.
    • 模式patt –表示要讀取的模式(patt)。
  • In the second case, next(String patt),

    在第二種情況下, next(String patt)

    • String patt – represents the string to define the pattern (patt) to read.
    • 字符串patt –表示用于定義要讀取的圖案(圖案)的字符串。

Return value:

返回值:

In all the cases, the return type of the method is String, it retrieves the next token

在所有情況下,方法的返回類型為String ,它檢索下一個標記

Example 1:

范例1:

// Java program is to demonstrate the example 
// of next() method of Scanner
import java.util.*;
import java.util.regex.*;
public class Next {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using next() method is to
// display the next complete 
// token
String next = sc.next();
System.out.println("sc.next(): " + next);
// Scanner closed
sc.close();
}
}

Output

輸出量

sc.next(): Java

Example 2:

范例2:

import java.util.*;
import java.util.regex.*;
public class Next {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using net(Pattern) method is
// to return the next token when it meets
// the given pattern
String next_p = sc.next(Pattern.compile("J..a"));
System.out.println("sc.next(Pattern.compile(J..a)): " + next_p);
// Scanner closed
sc.close();
}
}

Output

輸出量

sc.next(Pattern.compile(J..a)): Java

Example 3:

范例3:

import java.util.*;
import java.util.regex.*;
public class Next {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using net(String) method is
// to return the next token when it meets
// the given pattern formed from the given
// string
String next_s = sc.next("Java");
System.out.println("sc.next(Java)): " + next_s);
// Scanner closed
sc.close();
}
}

Output

輸出量

sc.next(Java)): Java

翻譯自: https://www.includehelp.com/java/scanner-next-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541005.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541005.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541005.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql怎樣查表的模式_mysql常用基礎操作語法(四)--對數據的簡單無條件查詢及庫和表查詢【命令行模式】...

1、mysql簡單的查詢:select 字段1,字段2... from tablename;如果字段那里寫一個*,代表查詢所有的字段,等同于指定出所有的字段名,因此如果要查詢所有字段的數據,一般都是用*。2、去重查詢:selec…

Google再次從官方商店下架偽裝成合法程序的惡意應用

本月內的第二次,Google 從官方應用商店 Google Play 移除了偽裝成合法程序的惡意應用。被移除的應用都屬于名叫 Ztorg 的 Android 惡意程序家族,能利用已知的漏洞 root 被感染的設備,使其很難被刪除。自去年 9 月以來,Ztorg 惡意應…

java scanner_Java Scanner skip()方法與示例

java scanner掃描儀類skip()方法 (Scanner Class skip() method) Syntax: 句法: public Scanner skip(Pattern patt);public Scanner skip(String patt);skip() method is available in java.util package. skip()方法在java.util包中可用。 skip(Pattern patt) me…

MySQL文件后_MySQL誤刪除文件后,如何恢復

MySQL在運行中,如果誤刪除數據文件,只有服務進程沒有退出,那么就有辦法將其恢復。首先介紹Linux下lsof:他可以顯示打開的文件和網絡連接。其次/proc目錄包含了反映內核和進程樹的各種文件。/proc/504目錄包含的是PID是504的進程信…

【載譽】致遠互聯榮獲“2017最佳協同管理解決方案”殊榮

6月15日,一年一度的大連軟交會于大連市世界博覽廣場盛大舉行。“2017企業服務創新論壇”作為軟交會最重要的組成部分之一,本年度以“守正出新——通往基業長青的數字化選擇”為主題,吸引到近200位企業級服務領域的企業家及高管參加。致遠互聯…

c++ cdi+示例_C ++中帶有示例的本地類

c cdi示例C 中的本地類 (Local Class in C) In C, generally a class is declared outside of the main() function, which is global for the program and all functions can access that class i.e. the scope of such class is global. 在C 中,通常在main()函數外…

RabbitMQ安裝|使用|概念|Golang開發

手冊:http://www.rabbitmq.com/getstarted.html 安裝:http://www.rabbitmq.com/download.html 參考:http://blog.csdn.net/whycold/article/details/41119807 一.介紹 AMQP,即Advanced Message Queuing Protocol,高級消息隊列協議&#xff0c…

python中的替換函數_python:替換模塊類中的函數

我試圖替換類中定義的函數,以便在不更改實際代碼的情況下修改其函數(如內部工作)。我以前從來沒有這樣做過,因此在更換它時遇到一些問題。更改代碼會讓我訪問python庫中的包,這不是一個很好的選擇。例如,如果模塊名為testmodclass testMOD(object):def testFunc(self, variable…

諾基亞AirScale支持低頻段和高頻段5G服務 確保運營商投資收入

據悉,諾基亞創新和測試強調了其AirScale無線產品組合的靈活性、可升級性和可擴展性,以適應技術初始應用中實現的5G頻段。 通過證明AirScale能夠支持低頻和高頻,運營商將能夠從5G推出的第一天提供廣泛的覆蓋和室內覆蓋,而無需進行復…

MS的完整形式是什么?

碩士:理學碩士/外科碩士/ MicroSoft (MS: Master of Science / Master of Surgery / MicroSoft) 1)碩士:理學碩士 (1) MS: Master of Science) MS is an abbreviation of Master of Science. It is a masters degree program provided by universities i…

java 線程安全的linkedlist_使ArrayList,LinkedList變成線程安全的

1.使用SynchronizedListSynchronizedList是一個線程安全的包裝類。繼承于SynchronizedCollection,SynchronizedCollection實現了Collection接口,SynchronizedList包含一個List對象,對List的訪問修改方法進行了一些封裝,在封裝的方…

人工智能能夠構建一個自主驅動云嗎?

企業和組織可以從云計算中受益,但許多公司并不希望面對公共云的成本,性能和治理問題,并且認為構建自己的私有云的復雜性和運營開銷并沒有那么困難。 如今,一些云計算供應商正在使用人工智能(AI)來簡化私有云…

scala中def_def關鍵字以及Scala中的示例

scala中defScala def關鍵字 (Scala def keyword) The def keyword in Scala is used to declare functions and methods in Scala. Scala being ignorant on the data types does the same with the return type of a function. Declaring and defining a function in Scala do…

前端必備的 web 安全知識手記

前言 安全這種東西就是不發生則已,一發生則驚人。作為前端,平時對這方面的知識沒啥研究,最近了解了下,特此沉淀。文章內容包括以下幾個典型的 web 安全知識點:XSS、CSRF、點擊劫持、SQL 注入和上傳問題等(…

php腳本開頭注釋_PHP文件注釋標記及規范小結

PHP文件注釋標記及規范小結發布時間:2016-06-17 來源: 點擊:次PHP 注釋標記access使用范圍:class,function,var,define,module該標記用于指明關鍵字的存取權限:private、public或protecedauthor指明作者copyright使用范圍&#xf…

Salesforce宣布5.82億美元收購文件編輯公司Quip

北京時間8月2日消息,據路透社報道,云軟件開發商Salesforce.com周一宣布,已同意以大約5.82億美元收購文件編輯創業公司Quip。 Salesforce此前已經對Quip進行了投資。Quip開發的文字處理平臺供企業員工用于在移動設備、可穿戴設備以及臺式機上編…

Java中的main()方法是強制性的嗎?

The question is that "Is main() method is compulsory in Java?" 問題是“ main()方法在Java中是強制性的嗎?” Yes, we can write a java program without main() method but there is a condition if and only if java JDK version till JDK 5. 是的…

php date( w ),PHP Date()函數詳解

頁面的最上方加上:date_default_timezone_set(PRC); /*把時間調到北京時間,php5默認為格林威治標準時間*/date ()a: "am"或是"pm"A: "AM"或是"PM"d: 幾日,兩位數字,若不足則補零&#xff1b…

10.6-全棧Java筆記:常見流詳解(四)

上節我們講到「Java中常用流:數據流和對象流」,本節我們學習文件字符流和文件緩沖流~文件字符流前面介紹的文件字節流可以處理所有的文件,但是字節流不能很好的處理Unicode字符,經常會出現“亂碼”現象。所以,我們處理…

python 示例_帶有示例的Python File open()方法

python 示例文件open()方法 (File open() Method) open() method is an inbuilt method in Python, it is used to create, open or append a file. open()方法是Python中的內置方法,用于創建,打開或附加文件。 Syntax: 句法: file_object …