Java Scanner nextShort() 方法



描述

java Scanner nextShort() 方法將輸入的下一個標記掃描為短整型。以 nextShort() 形式呼叫的此方法的行為與呼叫 nextShort(radix) 的行為完全相同,其中 radix 是此掃描程式的預設基數。

宣告

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

public short nextShort()

引數

返回值

此方法返回從輸入掃描的短整型。

異常

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

  • NoSuchElementException − 如果輸入已耗盡

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

Java Scanner nextShort(int radix) 方法

描述

Java Scanner nextShort(int radix) 方法將輸入的下一個標記掃描為短整型。如果下一個標記無法按照以下說明轉換為有效的短整型值,則此方法將丟擲 InputMismatchException。如果轉換成功,掃描程式將越過匹配的輸入。

宣告

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

public short nextShort(int radix)

引數

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

返回值

此方法返回從輸入掃描的短整型。

異常

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

  • NoSuchElementException − 如果輸入已耗盡

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

使用字串示例獲取掃描程式的下一個短整型標記

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

使用給定基數的字串掃描程式獲取下一個短整型標記的示例

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

使用使用者輸入示例獲取掃描程式的下一個短整型標記

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

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

輸出

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

3
Found: 3
java_util_scanner.htm
廣告