当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Java.util.Scanner.hasNext()用法及代码示例


描述

这个java.util.Scanner.hasNext()方法 如果此扫描器的输入中有另一个标记,则返回 true。此方法可能会在等待输入扫描时阻塞。扫描仪不会通过任何输入。

声明

以下是声明java.util.Scanner.hasNext()方法

public boolean hasNext()

参数

NA

返回值

当且仅当此扫描器有另一个令牌时,此方法才返回 true

异常

IllegalStateExceptionâˆ' 如果此扫描仪已关闭

示例

下面的例子展示了 java.util.Scanner.hasNext() 方法的用法。

package com.tutorialspoint;

import java.util.*;
import java.util.regex.Pattern;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // check if the scanner has a token
      System.out.println("" + scanner.hasNext());

      // print the rest of the string
      System.out.println("" + scanner.nextLine());

      // check if the scanner has a token after printing the line
      System.out.println("" + scanner.hasNext());

      // close the scanner
      scanner.close();
   }
}

让我们编译并运行上面的程序,这将产生以下结果——

true
Hello World! 3 + 3.0 = 6
false

相关用法


注:本文由纯净天空筛选整理自 Java.util.Scanner.hasNext() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。