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


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


描述

這個java.util.Scanner.nextBigInteger()方法將輸入的下一個標記作為 BigInteger 進行掃描。以 nextBigInteger() 形式調用此方法的行為與調用 nextBigInteger(radix) 完全相同,其中 radix 是此掃描器的默認基數。

聲明

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

public BigInteger nextBigInteger()

參數

NA

返回值

此方法返回從輸入掃描的 BigInteger

異常

  • InputMismatchException- 如果下一個標記與整數正則表達式不匹配,或者超出範圍

  • NoSuchElementException- 如果輸入用盡

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

示例

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

package com.tutorialspoint;

import java.util.*;

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

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

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

      // find the next BigInteger token and print it
      // loop for the whole scanner
      while (scanner.hasNext()) {

         // if the next is BigInteger, print "Found" and the Integer
         if (scanner.hasNextBigInteger()) {
            System.out.println("Found:" + scanner.nextBigInteger());
         }

         // if a BigInteger is not found, print "Not Found" and the token
         System.out.println("Not Found:" + scanner.next());
      }

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

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

Not Found:Hello
Not Found:World!
Found:3
Not Found:+
Not Found:3.0
Not Found:=
Found:6
Not Found:true

相關用法


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