java 方法 示例
扫描器类的hasNextLong()方法 (Scanner Class hasNextLong() method)
Syntax:
句法:
public boolean hasNextLong();
public boolean hasNextLong(int rad);
hasNextLong() method is available in java.util package.
hasNextLong()方法在java.util包中可用。
hasNextLong() method is used to check whether this Scanner has next token in its input can be manipulated as a long in the implicit radix or not.
hasNextLong()方法用于检查此扫描程序在其输入中是否具有下一个标记,是否可以将其操纵为隐含基数中的long值。
hasNextLong(int rad) method is used to check whether this Scanner has next token in its input can be manipulated as a long value in the explicit or given radix (rad) or not.
hasNextLong(int rad)方法用于检查此扫描程序在其输入中是否具有下一个标记,可以将其作为显式值或给定基数(rad)中的long值进行操作。
These methods may throw an exception at the time of representing input as a long value.
在将输入表示为长值时,这些方法可能会引发异常。
IllegalStateException: This exception may throw when this Scanner is not opened.
IllegalStateException :如果未打开此扫描器,则可能引发此异常。
These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问;如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, hasNextLong(),
在第一种情况下, hasNextLong() ,
- It does not accept any parameter.
In the second case, hasNextLong(int rad),
在第二种情况下, hasNextLong(int rad) ,
- int rad – represents the radix used to manipulate as a long value.
- int rad –表示用于作为长值操作的基数。
Return value:
返回值:
In both the cases, the return type of the method is boolean, it returns true when this Scanner next input as a long value otherwise it returns false.
在这两种情况下,方法的返回类型均为boolean ,当此Scanner下次输入为long值时,该方法返回true ,否则返回false 。
Example:
例:
// Java program is to demonstrate the
// example of hasNextLong() method of
// Scanner
import java.util.*;
import java.util.regex.*;
public class HasNextLong {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
Long val = 124582456l;
str = str + val;
// Instantiates Scanner
Scanner sc = new Scanner(str);
while (sc.hasNext()) {
// By using hasNextLong() method is to
// check whether this object next token
// represents long or not in the default
// radix
boolean status = sc.hasNextLong();
System.out.println("sc.hasNextLong(): " + status);
// By using hasNextLong(radix) method is to
// check whether this object next token
// represents integer in the given radix
// or not
status = sc.hasNextLong(4);
System.out.println("sc.hasNextLong(2): " + status);
sc.next();
}
// Scanner closed
sc.close();
}
}
Output
输出量
sc.hasNextLong(): false
sc.hasNextLong(2): false
sc.hasNextLong(): false
sc.hasNextLong(2): false
sc.hasNextLong(): true
sc.hasNextLong(2): true
sc.hasNextLong(): false
sc.hasNextLong(2): false
sc.hasNextLong(): false
sc.hasNextLong(2): false
sc.hasNextLong(): true
sc.hasNextLong(2): false
翻译自: https://www.includehelp.com/java/scanner-hasnextlong-method-with-example.aspx
java 方法 示例