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()方法用于读取输入的下一个令牌,作为此Scanner的隐含基数处的长值。

  • 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的显式或给定基数(rad)处读取输入的下一个标记作为长值。

  • These methods may throw an exception at the time of representing an input as a long value.

    在将输入表示为长值时,这些方法可能会引发异常。

    • InputMismatchException: This exception may throw when the next token of the input mismatch.InputMismatchException :当输入的下一个标记不匹配时,可能引发此异常。
    • NoSuchElementException: This exception may throw when no such element exists.NoSuchElementException :如果不存在这样的元素,则可能引发此异常。
    • IllegalStateException: This exception may throw when this Scanner is not opened.IllegalStateException :如果未打开此扫描器,则可能引发此异常。
  • 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.

    这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • 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 –表示用于操纵令牌的基数为长值。

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 Scanner

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 Scanner
  Scanner sc = new Scanner(str);

  while (sc.hasNext()) {
   // By using nextLong() method isto
   // return the next token as a 
   // long at the implicit radix
   if (sc.hasNextLong()) {
    long next_l = sc.nextLong();
    System.out.println("sc.nextLong()): " + next_l);
   }
   System.out.println(sc.next());
  }

  // Scanner closed
  sc.close();
 }
}

Output

输出量

Java
Programming!
sc.nextLong()): 3
*
8=
sc.nextLong()): 24
+
b

Example 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 Scanner
  Scanner sc = new Scanner(str);

  while (sc.hasNext()) {
   // By using nextLong(9) method isto
   // return the next token as a 
   // long at the explicit radix
   if (sc.hasNextLong()) {
    long next_l = sc.nextLong(9);
    System.out.println("sc.nextLong(9)): " + next_l);
   }
   System.out.println(sc.next());
  }

  // Scanner closed
  sc.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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值