java scanner_Java Scanner nextLong()方法与示例
java scanner
掃描器類的nextLong()方法 (Scanner Class nextLong() method)
Syntax:
句法:
public long nextLong();public long nextLong(int rad);nextLong() method is available in java.util package.
nextLong()方法在java.util包中可用。
nextLong() method is used to read the next token of the input as a long value at the implicit radix of this Scanner.
nextLong()方法用于讀取輸入的下一個(gè)令牌,作為此Scanner的隱含基數(shù)處的長(zhǎng)值。
nextLong(int rad) method is used to read the next token of the input as a long value at the explicit or given radix(rad) of this Scanner.
nextLong(int rad)方法用于在此Scanner的顯式或給定基數(shù)(rad)處讀取輸入的下一個(gè)標(biāo)記作為長(zhǎng)值。
These methods may throw an exception at the time of representing an input as a long value.
在將輸入表示為長(zhǎng)值時(shí),這些方法可能會(huì)引發(fā)異常。
- InputMismatchException: This exception may throw when the next token of the input mismatch.InputMismatchException :當(dāng)輸入的下一個(gè)標(biāo)記不匹配時(shí),可能引發(fā)此異常。
- NoSuchElementException: This exception may throw when no such element exists.NoSuchElementException :如果不存在這樣的元素,則可能引發(fā)此異常。
- IllegalStateException: This exception may throw when this Scanner is not opened.IllegalStateException :如果未打開(kāi)此掃描器,則可能引發(fā)此異常。
These are non-static methods and it is accessible with the class object only and if we try to access these methods with the class name then we will get an error.
這些是非靜態(tài)方法,只能通過(guò)類對(duì)象訪問(wèn),如果嘗試使用類名稱訪問(wèn)這些方法,則會(huì)收到錯(cuò)誤消息。
Parameter(s):
參數(shù):
In the first case, nextLong(),
在第一種情況下, nextLong() ,
- It does not accept any parameter.
In the second case, nextLong(int rad),
在第二種情況下, nextLong(int rad) ,
- int rad – represents the radix used to manipulate the token as a long value.
- int rad –表示用于操縱令牌的基數(shù)為長(zhǎng)值。
Return value:
返回值:
In both the cases, the return type of the method is long, it retrieves the long value read from the input.
在這兩種情況下,方法的返回類型都是long ,它檢索從輸入讀取的long值。
Example 1:
范例1:
// Java program to demonstrate the example // of nextLong() method of Scannerimport java.util.*; import java.util.regex.*;public class NextLong {public static void main(String[] args) {String str = "Java Programming! 3 * 8= 24 + b";long l = 101245l;// Instantiates ScannerScanner sc = new Scanner(str);while (sc.hasNext()) {// By using nextLong() method isto// return the next token as a // long at the implicit radixif (sc.hasNextLong()) {long next_l = sc.nextLong();System.out.println("sc.nextLong()): " + next_l);}System.out.println(sc.next());}// Scanner closedsc.close();} }Output
輸出量
Java Programming! sc.nextLong()): 3 * 8= sc.nextLong()): 24 + bExample 2:
范例2:
import java.util.*; import java.util.regex.*;public class NextLong {public static void main(String[] args) {String str = "Java Programming! 3 * 8= 24 + b";long l = 101245l;// Instantiates ScannerScanner sc = new Scanner(str);while (sc.hasNext()) {// By using nextLong(9) method isto// return the next token as a // long at the explicit radixif (sc.hasNextLong()) {long next_l = sc.nextLong(9);System.out.println("sc.nextLong(9)): " + next_l);}System.out.println(sc.next());}// Scanner closedsc.close();} }Output
輸出量
Java Programming! sc.nextLong(9)): 3 * 8= sc.nextLong(9)): 22 + b翻譯自: https://www.includehelp.com/java/scanner-nextlong-method-with-example.aspx
java scanner
總結(jié)
以上是生活随笔為你收集整理的java scanner_Java Scanner nextLong()方法与示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 颐和园学生票门票多少钱
- 下一篇: “首路或参差”下一句是什么