java 方法 示例_Java扫描仪具有示例的NextNextInt()方法

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 方法 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值