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


Java Java.util.Scanner.next()用法及代碼示例


描述

這個java.util.Scanner.next()方法從這個掃描器中查找並返回下一個完整的標記。一個完整的標記前後是與分隔符模式匹配的輸入。此方法可能會在等待輸入掃描時阻塞,即使先前調用 hasNext() 返回 true。

聲明

以下是聲明java.util.Scanner.next()方法

public String next()

參數

NA

返回值

此方法返回下一個令牌

異常

  • NoSuchElementException- 如果沒有更多的令牌可用

  • IllegalStateException- 如果此掃描儀已關閉

示例

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

package com.tutorialspoint;

import java.util.*;

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);

      // find the next token and print it
      System.out.println("" + scanner.next());

      // find the next token and print it
      System.out.println("" + scanner.next());

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

讓我們編譯並運行上麵的程序,這將產生以下結果 -

Hello
World!

相關用法


注:本文由純淨天空篩選整理自 Java.util.Scanner.next() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。