當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。