Java Scanner nextInt() 方法



描述

java Scanner nextInt() 方法掃描下一個標記作為 int。此方法的呼叫形式 nextInt() 的行為與呼叫 nextInt(radix) 完全相同,其中 radix 是此掃描器的預設基數。

宣告

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

public int nextInt()

引數

返回值

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

異常

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

  • NoSuchElementException - 如果輸入耗盡

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

Java Scanner nextInt(int radix) 方法

描述

java.util.Scanner.nextInt(int radix) 方法掃描下一個標記作為 int。如果下一個標記無法轉換為下面描述的有效 int 值,則此方法將丟擲 InputMismatchException。如果轉換成功,則掃描器將前進到匹配的輸入。

宣告

以下是 java.util.Scanner.nextInt(int radix) 方法的宣告

public int nextInt(int radix)

引數

radix - 用於將標記解釋為 int 值的基數

返回值

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

異常

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

  • NoSuchElementException - 如果輸入耗盡

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

在字串上的 Scanner 獲取下一個標記作為 Int 的示例

以下示例演示瞭如何使用 Java Scanner nextInt() 方法以預設基數掃描下一個標記作為 Int。我們使用給定的字串建立了一個掃描器物件。然後我們檢查每個標記是否為 Int 並列印,否則列印“未找到”以及掃描的標記。最後,使用 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 Int
         if(scanner.hasNextInt()){
            // print what is scanned
            System.out.println("Found: " + scanner.nextInt());		 
         } 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

以給定基數獲取下一個標記作為 Int 的 Scanner 字串示例

以下示例演示瞭如何使用 Java Scanner nextInt() 方法以給定的基數掃描下一個標記作為 Int。我們使用給定的字串建立了一個掃描器物件。然後我們檢查每個標記是否為 Int 並列印,否則列印“未找到”以及掃描的標記。最後,使用 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 Int
         if(scanner.hasNextInt(4)){
            // print what is scanned
            System.out.println("Found: " + scanner.nextInt(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 獲取下一個標記作為 Int 的示例

以下示例演示瞭如何使用 Java Scanner nextInt() 方法以給定的基數掃描下一個標記作為 Int。我們使用 System.in 類建立了一個掃描器物件。然後我們檢查每個標記是否為 Int 並列印,否則列印“未找到”以及掃描的標記。最後,使用 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 Int
      if(scanner.hasNextInt(4)){
         // print what is scanned
         System.out.println("Found: " + scanner.nextInt(4));		 
      } else {
         System.out.println("Not Found: " + scanner.next());
      }

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

輸出

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

3
Found: 3
java_util_scanner.htm
廣告