博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Scanner next()方法与示例
阅读量:2530 次
发布时间:2019-05-11

本文共 3416 字,大约阅读时间需要 11 分钟。

扫描仪类的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 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

翻译自:

转载地址:http://hvtzd.baihongyu.com/

你可能感兴趣的文章
linux快捷键绝对路径相对路径讲解
查看>>
又漏一次
查看>>
dede模板中plus文件路径使用方法
查看>>
xml解析demo使用
查看>>
python使用模板手记
查看>>
No result defined for action com.nynt.action.ManageAction and result input问题
查看>>
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
查看>>
洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
查看>>
c++ 连接数据库
查看>>
函数指针与回调函数
查看>>
你所不知道的 CSS 滤镜技巧与细节
查看>>
hack reviewboard 让 FireFox 把 tab 显示为 4 或 2 个空格
查看>>
css 学习整理
查看>>
录制游戏视频——fraps
查看>>
jQuery 停止动画
查看>>
HTML基础之JS
查看>>
01背包问题python 2.7实现
查看>>
E - Kagome Kagome
查看>>
Android Studio Errors
查看>>
软件工程第二次作业—结对编程
查看>>