Java Scanner nextByte() Method
Example
Print the value of every byte in the string:
// Create a scanner object
Scanner myObj = new Scanner("A byte is a number between -128 and 127");
// Print the value of every byte in the scanner
while (myObj.hasNext()) {
if (myObj.hasNextByte()) {
System.out.println(myObj.nextByte());
} else {
myObj.next();
}
}
Definition and Usage
The nextByte()
method returns the byte
value of the number that the next token represents. The token must represent a whole number between -128 and 127.
If the radix parameter is used, then it interprets numbers using the radix. For example, a radix of 16 would interpret numbers as hexadecimal (digits 0 to 9 and A to F). If the radix parameter is not used then it interprets numbers using the scanner's radix, which is 10 by default but it can be changed with the useRadix()
method.
What is a token?
A token is a sequence of characters separated from other tokens by delimiters. The default delimiter is a block of whitespace characters but it can be changed with the useDelimiter()
method.
Syntax
One of the following:
public boolean nextByte()
public boolean nextByte(int radix)
Parameter Values
Parameter | Description |
---|---|
radix | Optional. Specifies the radix used to interpret numbers. The radix specifies how many different symbols can be used to represent a digit in a number. |
Technical Details
Returns: | The byte value of the number that the next token represents. |
---|---|
Throws: |
InputMismatchException - If the token does not represent a byte type value.NoSuchElementException - If there are no more tokens in the scanner.IllegalStateException - If the scanner has been closed.
|