java 方法 示例

扫描器类的hasNextInt()方法 (Scanner Class hasNextInt() method)

Syntax:

句法:

    public boolean hasNextInt();
public boolean hasNextInt(int rad);

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

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

  • hasNextInt() method is used to check whether this Scanner has next token in its input can be manipulated as an int in the implicit radix (rad) or not.

    hasNextInt()方法用于检查此扫描程序在其输入中是否具有下一个标记是否可以作为隐式基数(rad)中的int进行操作。

  • hasNextInt(int rad) method is used to check whether this Scanner has next token in its input can be manipulated as an int in the explicit or given radix (rad) or not.

    hasNextInt(int rad)方法用于检查此扫描程序在其输入中是否具有下一个标记,可以将其作为显式或给定基数(rad)中的int进行操作。

  • These methods may throw an exception at the time of representing input as an int.

    在将输入表示为int时,这些方法可能会引发异常。

    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, hasNextInt(),

    在第一种情况下, hasNextInt()

    • It does not accept any parameter.
  • In the second case, hasNextInt(int rad),

    在第二种情况下, hasNextInt(int rad)

    • int rad – represents the radix used to manipulate as an int.
    • int rad –表示用作int的基数。

Return value:

返回值:

In both the cases, the return type of the method is boolean, it returns true when this Scanner next input as an int otherwise it returns false.

在这两种情况下,方法的返回类型是布尔值 ,当这种扫描仪下一个输入为一个int否则返回false返回true。

Example:

例:

// Java program is to demonstrate the example
// of hasNextInt() method of Scanner
import java.util.*;
import java.util.regex.*;
public class HasNextInt {public static void main(String[] args) {String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
while (sc.hasNext()) {// By using hasNextInt() method is to
// check whether this object next token
// represents int or not in the default
// radix
boolean status = sc.hasNextInt();
System.out.println("sc.hasNextInt(): " + status);
// By using hasNextInt(radix) method is to
// check whether this object next token
// represents integer in the given radix
// or not
status = sc.hasNextInt(2);
System.out.println("sc.hasNextInt(2): " + status);
sc.next();
}
// Scanner closed
sc.close();
}
}

Output

输出量

sc.hasNextInt(): false
sc.hasNextInt(2): false
sc.hasNextInt(): false
sc.hasNextInt(2): false
sc.hasNextInt(): true
sc.hasNextInt(2): false
sc.hasNextInt(): false
sc.hasNextInt(2): false
sc.hasNextInt(): false
sc.hasNextInt(2): false
sc.hasNextInt(): true
sc.hasNextInt(2): false

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

java 方法 示例

java 方法 示例_Java扫描仪具有示例的NextNextInt()方法相关推荐

  1. java 方法 示例_Java扫描仪的hasNextBoolean()方法与示例

    java 方法 示例 扫描器类的hasNextBoolean()方法 (Scanner Class hasNextBoolean() method) hasNextBoolean() method i ...

  2. java 静态缓存示例_Java 9 JShell示例:集合静态工厂方法

    java 静态缓存示例 这篇文章继续从My My Java 9 Features博客文章中探索Java9功能. 在这里,我们在List,Set和Map接口中试验Java9 Collections静态工 ...

  3. java泛型方法 通配符_Java泛型教程–示例类,接口,方法,通配符等

    java泛型方法 通配符 泛型是Java编程的核心功能之一,它是Java 5中引入的.如果您使用的是Java Collections ,并且版本5或更高版本,则可以肯定使用了它. 将泛型与集合类一起使 ...

  4. java 示例_Java最终关键字示例

    java 示例 The final keyword in Java can be used with variables, methods, and classes. Java中的final关键字可以 ...

  5. java 生成随机数_Java 生成随机数的 N 种方法

    原标题:Java 生成随机数的 N 种方法 www.baeldung.com/java-generating-random-numbers 1.引言 本文将探讨用 Java 生成随机数的不同方法. 2 ...

  6. java反射方法调用_Java反射(3)调用方法

    目录: 1.访问方法(Method)对象的方法汇总 2.获取方法(Method)对象中的值 3.设置方法(Method)对象中的值 1.访问Method的方法汇总 以下四种方法在Class类可以返回关 ...

  7. java get方法使用_java中关于set()和get()方法的理解和使用

    java中 当定义了一个私有的成员变量的时候,若是须要访问或者获取这个变量的时候,就能够编写set或者get方法去调用.java set()是给属性赋值的,get()是取得属性值的 被设置和存取的属性 ...

  8. java泛型方法作用_java泛型的作用与使用方法是什么?

    泛型,即"参数化类型".一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参.那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,类似于方法中的变 ...

  9. java 随机数生成实现_Java中生成随机数的实现方法总结

    搜索热词 在实际开发工作中经常需要用到随机数.如有些系统中创建用户后会给用户一个随机的初始化密码.这个密码由于是随机的,为此往往只有用户自己知道.他们获取了这个随机密码之后,需要马上去系统中更改.这就 ...

最新文章

  1. RedisDesktopManager连接不上redis的解决方法
  2. 正则表达式基础知识及应用(用于个人学习以及回顾)
  3. C++:Static修饰变量 vs Static修饰函数
  4. win7发现不了无线网络怎么办 win7发现不了无线网络的解决办法
  5. npm报错 npm WARN deprecated....的问题解决方式
  6. 看完这些美食海报,你是不是又有灵感了?
  7. java固定资产管理系统_基于jsp的固定资产管理系统-JavaEE实现固定资产管理系统 - java项目源码...
  8. Windows下PHP安装配置
  9. Windows 下 docker 部署 gitlab ci
  10. 互联网架构介绍 --from 光荣之路
  11. 卡巴斯基离线病毒库升级办法
  12. 嵌入式硬件:电子元器件(1)电阻电容电感
  13. 大华服务器系统配置图,大华磁盘阵列配置说明指南.doc
  14. 被动语态 动词的过去分词
  15. Problem A: 小勇学分数
  16. mac系统下查看端口占用问题的解决方案
  17. python socket
  18. 查找整数 本题要求从输入的N个整数中查找给定的X
  19. NOIP 2018 提高组初赛试题 题目+答案+简要解析
  20. 使用 Redis 实现 Feed 流

热门文章

  1. php导出excel数据代码,phpspreadsheet导出数据到Excel的方法介绍(代码示例)
  2. linux常用命令--开发调试篇
  3. TensorFlow构建二维数据拟合模型(2)
  4. 防火墙firewalld
  5. Java并发篇_synchronized
  6. MySQL Index Condition Pushdown
  7. 使用Mybatis Generator结合Ant脚本快速自动生成Model、Mapper等文件的方法
  8. append生成新变量的时候,没有如预期(It's a feature,not a bug?)
  9. eclipse中项目内存溢出问题
  10. (转)新ITC提交APP常见问题与解决方法(Icon Alpha,Build version,AppIcon120x120)(2014-11-17)...