Java Scanner hasNextBigInteger() 方法



描述

Java Scanner hasNextBigInteger() 方法返回 true,如果此掃描程式輸入中的下一個標記可以使用 nextBigInteger() 方法解釋為預設基數的 BigInteger。掃描程式不會跳過任何輸入。

宣告

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

public boolean hasNextBigInteger()

引數

返回值

當且僅當此掃描程式的下一個標記是有效的 BigInteger 時,此方法返回 true。

異常

IllegalStateException − 如果此掃描程式已關閉

Java Scanner hasNextBigInteger(int radix)

描述

Java Scanner hasNextBigInteger(int radix) 方法返回 true,如果此掃描程式輸入中的下一個標記可以使用 nextBigInteger() 方法解釋為指定基數的 BigInteger。掃描程式不會跳過任何輸入。

宣告

以下是 Java Scanner hasNextBigInteger() 方法的宣告

public boolean hasNextBigInteger(int radix)

引數

radix − 用於將標記解釋為整數的基數

返回值

當且僅當此掃描程式的下一個標記是有效的 BigInteger 時,此方法返回 true。

異常

IllegalStateException − 如果此掃描程式已關閉

使用 Scanner 對字串示例檢查下一個標記作為 BigInteger

以下示例演示了 Java Scanner hasNextBigInteger() 方法的使用,用於使用預設基數檢查下一個標記是否為 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
         System.out.println("" + scanner.hasNextBigInteger());

         // print what is scanned
         System.out.println("" + scanner.next());
      }

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

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

false
Hello
false
World!
true
3
false
+
false
3.0
false
=
true
6

使用 Scanner 對字串示例檢查基數為 5 的下一個標記作為 BigInteger

以下示例演示了 Java Scanner hasNextBigInteger() 方法的使用,用於使用基數 5 檢查下一個標記是否為 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
         System.out.println("" + scanner.hasNextBigInteger(4));

         // print what is scanned
         System.out.println("" + scanner.next());
      }

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

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

false
Hello
false
World!
true
3
false
+
false
3.0
false
=
false
6

使用 Scanner 對使用者輸入示例檢查下一個標記作為 BigInteger

以下示例演示了 Java Scanner hasNextBigInteger() 方法的使用,用於檢查下一個標記是否為 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()){
         // print what is scanned
         System.out.println(scanner.next());		 
      } else {
         scanner.next();
      }

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

輸出

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

3
3
java_util_scanner.htm
廣告
© . All rights reserved.