Java Scanner hasNextByte() MethodThe hasNextByte() is a Java Scanner class method which is used to check the next token in this scanner's input can be interpreted as a byte value in the default radix or not. It returns true if it is interprated as a byte value, otherwise returns false. There is two different types of Java hasNextByte() method which can be differentiated depending on its parameter. These are:
hasNextByte() Method:This is a Java Scanner class method which is used to check the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method. hasNextByte(int radix) Method:This is an inbuilt method of Java Scanner class which is used to check the next token in this scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method. SyntaxFollowing is the declaration of hasNextByte() method: Parameter
ReturnsThe hasNextByte() method returns true if and only if this scanner's next token is a valid byte value. ExceptionsIllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed. IllegalArgumentException- It throws this exception if the specified radix is out of range. Compatibility VersionJava 1.5 and above Example 1Output: Scan String: JavaTpoint.com Result: true Scan String: 11 Result: false Scan String: + Result: true Scan String: 11 Result: false Scan String: = Result: false Scan String: 22.0 Result: false Example 2Output: Found a byte! Example 3Output: Not Found :JavaTpoint.com Found :7 Not Found :+ Found :7 Not Found := Not Found :22.0 Example 4Output: Exception in thread "main" java.lang.IllegalArgumentException: radix:598670 at java.base/java.util.Scanner.setRadix(Scanner.java:1368) at java.base/java.util.Scanner.nextByte(Scanner.java:1998) at myPackage.ScannerHasNextByteExample4.main(ScannerHasNextByteExample4.java:10) Next TopicJava-scanner-hasnextdouble-method |