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.mzph.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 …