Java Scanner nextLong() 方法



描述

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

宣告

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

public long nextLong()

引數

返回值

此方法返回從輸入掃描的長整型數

異常

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

  • NoSuchElementException − 如果輸入已耗盡

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

Java Scanner nextLong(int radix) 方法

描述

Java Scanner nextLong(int radix) 方法掃描輸入的下一個標記作為長整型數。如果下一個標記不能如以下所述轉換為有效長整型值,則此方法將丟擲 InputMismatchException。如果轉換成功,則掃描器將前進到匹配的輸入。

宣告

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

public long nextLong(int radix)

引數

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

返回值

此方法返回從輸入掃描的長整型數

異常

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

  • NoSuchElementException − 如果輸入已耗盡

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

Java Scanner nextLong(int radix) 方法

描述

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

宣告

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

public int nextLong(int radix)

引數

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

返回值

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

異常

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

  • NoSuchElementException − 如果輸入已耗盡

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

在字串上使用 Scanner 獲取下一個標記作為長整型數的示例

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

以基數獲取 Scanner 上字串的下一個標記作為長整型數的示例

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

在使用者輸入上使用 Scanner 獲取下一個標記作為長整型數的示例

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

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

輸出

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

13964599874
Found: 13964599874
java_util_scanner.htm
廣告