扫描器类的hasNextByte()方法 (Scanner Class hasNextByte() method)
Syntax:
句法:
public boolean hasNextByte();
public boolean hasNextByte(int rad);
hasNextByte() method is available in java.util package.
hasNextByte()方法在java.util包中可用。
hasNextByte() method is used to check whether this Scanner has next token in its input can be manipulated as a byte in the implicit radix (rad) or not.
hasNextByte()方法用于检查此扫描程序在其输入中是否具有下一个标记,可以将其作为隐式基数(rad)中的字节进行操作。
hasNextByte(int rad) method is used to check whether this Scanner has next token in its input can be manipulated as a byte in the explicit or given radix (rad) or not.
hasNextByte(int rad)方法用于检查此扫描程序在其输入中是否具有下一个标记,可以将其作为显式或给定基数(rad)中的字节进行操作。
These methods may throw an exception at the time of representing input as a byte.
这些方法在将输入表示为字节时可能会引发异常。
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, hasNextByte(),
在第一种情况下, hasNextByte() ,
- It does not accept any parameter.
In the second case, hasNextByte(int rad),
在第二种情况下, hasNextByte(int rad) ,
- int rad – represents the radix used to manipulate as a byte.
- int rad –表示用于作为字节进行操作的基数。
Return value:
返回值:
In both the cases, the return type of the method is boolean, it returns true when this Scanner input is a valid byte otherwise it returns false.
在这两种情况下,方法的返回类型是布尔值 ,当这种扫描仪输入是有效的字节否则返回false返回true。
Example:
例:
// Java program is to demonstrate the example
// of hasNextByte() method of Scanner
import java.util.*;
import java.util.regex.*;
public class HasNextByte {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
while (sc.hasNext()) {
// By using hasNextByte() method is to
// check whether this object next token
// represents byte or not in the default
// radix
boolean status = sc.hasNextByte();
System.out.println("sc.hasNextByte(): " + status);
// By using hasNextByte() method is to
// check whether this object next token
// represents byte in the given radix
// or not
status = sc.hasNextByte(2);
System.out.println("sc.hasNextByte(2): " + status);
sc.next();
}
// Scanner closed
sc.close();
}
}
Output
输出量
sc.hasNextByte(): false
sc.hasNextByte(2): false
sc.hasNextByte(): false
sc.hasNextByte(2): false
sc.hasNextByte(): true
sc.hasNextByte(2): false
sc.hasNextByte(): false
sc.hasNextByte(2): false
sc.hasNextByte(): false
sc.hasNextByte(2): false
sc.hasNextByte(): true
sc.hasNextByte(2): false
翻译自: https://www.includehelp.com/java/scanner-hasnextbyte-method-with-example.aspx