日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Scanner next()方法与示例

發布時間:2025/3/11 java 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 Scannerimport java.util.*; import java.util.regex.*;public class Next {public static void main(String[] args) {String str = "Java Programming! 3 * 8= 24";// Instantiates ScannerScanner sc = new Scanner(str);// By using next() method is to// display the next complete // tokenString next = sc.next();System.out.println("sc.next(): " + next);// Scanner closedsc.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 ScannerScanner sc = new Scanner(str);// By using net(Pattern) method is// to return the next token when it meets// the given patternString next_p = sc.next(Pattern.compile("J..a"));System.out.println("sc.next(Pattern.compile(J..a)): " + next_p);// Scanner closedsc.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 ScannerScanner 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// stringString next_s = sc.next("Java");System.out.println("sc.next(Java)): " + next_s);// Scanner closedsc.close();} }

Output

輸出量

sc.next(Java)): Java

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

總結

以上是生活随笔為你收集整理的Java Scanner next()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。