Java Scanner nextBigInteger() 方法



描述

Java Scanner nextBigInteger() 方法將輸入的下一個標記掃描為 BigInteger。此方法的 nextBigInteger() 形式的呼叫與 nextBigInteger(radix) 呼叫完全相同,其中 radix 是此掃描器的預設基數。

宣告

以下是java.util.Scanner.nextBigInteger() 方法的宣告

public BigInteger nextBigInteger()

引數

返回值

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

異常

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

  • NoSuchElementException − 如果輸入已用盡

  • IllegalStateException − 如果此掃描器已關閉

Java Scanner nextBigInteger(int radix) 方法

描述

java.util.Scanner.nextBigInteger(int radix) 方法將輸入的下一個標記掃描為 BigInteger。如果下一個標記與上面定義的整數正則表示式匹配,則該標記將轉換為 BigInteger 值,方法是:移除所有組分隔符,透過 Character.digit 將非 ASCII 數字對映到 ASCII 數字,並將結果字串傳遞到 BigInteger(String, int) 建構函式,並使用指定的基數。

宣告

以下是java.util.Scanner.nextBigInteger() 方法的宣告

public BigInteger nextBigInteger(int radix)

引數

radix − 用於解釋標記的基數

返回值

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

異常

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

  • NoSuchElementException − 如果輸入已用盡

  • IllegalStateException − 如果此掃描器已關閉

基於字串的 Scanner 獲取下一個 BigInteger 標記的示例

以下示例演示瞭如何使用 Java Scanner nextBigInteger() 方法以預設基數掃描下一個標記為 BigInteger。我們使用給定的字串建立了一個掃描器物件。然後,我們檢查每個標記是否為 BigInteger,如果是,則打印出來;否則,列印“未找到”以及掃描到的標記。最後,使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

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

      while (scanner.hasNext()) {
         
         // check if the scanner's next token is a BigInteger
         if(scanner.hasNextBigInteger()){
            // print what is scanned
            System.out.println("Found: " + scanner.nextBigInteger());		 
         } else {
            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

使用指定基數獲取下一個 BigInteger 標記的 Scanner 字串示例

以下示例演示瞭如何使用 Java Scanner nextBigInteger() 方法以給定的基數掃描下一個標記為 BigInteger。我們使用給定的字串建立了一個掃描器物件。然後,我們檢查每個標記是否為 BigInteger,如果是,則打印出來;否則,列印“未找到”以及掃描到的標記。最後,使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

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

      while (scanner.hasNext()) {
         
         // check if the scanner's next token is a BigInteger
         if(scanner.hasNextBigInteger(4)){
            // print what is scanned
            System.out.println("Found: " + scanner.nextBigInteger(4));		 
         } else {
            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: =
Not Found: 6

基於使用者輸入的 Scanner 獲取下一個 BigInteger 標記的示例

以下示例演示瞭如何使用 Java Scanner nextBigInteger() 方法以給定的基數掃描下一個標記為 BigInteger。我們使用 System.in 類建立了一個掃描器物件。然後,我們檢查每個標記是否為 BigInteger,如果是,則打印出來;否則,列印“未找到”以及掃描到的標記。最後,使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

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

      // create a new scanner with System Input
      Scanner scanner = new Scanner(System.in);
         
      // check if the scanner's next token is a BigInteger
      if(scanner.hasNextBigInteger(4)){
         // print what is scanned
         System.out.println("Found: " + scanner.nextBigInteger(4));		 
      } else {
         System.out.println("Not Found: " + scanner.next());
      }

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

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們輸入了 3)

3
Found: 3
java_util_scanner.htm
廣告