Java Scanner nextShort() MethodThe nextShort() is a Java Scanner class method which is used to scan the next token of the input as a Short. There is two different types of Java nextShort() method which can be differentiated depending on its parameter. These are:
nextShort() MethodThis is a Java Scanner class method which is used to scan the next token of the input as a Short in the default radix. nextShort(int radix) MethodThis is an inbuilt method of Java Scanner class which is used to scan the next token of the input as a Short in the specified radix. SyntaxFollowing is the declaration of nextShort() method: Parameter
ReturnsThe nextShort() method returns the Short value scanned from the input. 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. InputMismatchException- It throws this exception if the next token does not match the Integer regular expression, or is out of range. Compatibility VersionJava 1.5 and above Example 1Output: Enter the Number you want: 40 Output value: 840 Example 2Output: Not Found :Facebook.com Found :7 Not Found :+ Found :7 Not Found := Not Found :26.0 Not Found :true Example 3Output: Not Found: JavaTpoint.com Found: 13 Not Found: + Found: 13 Not Found: = Not Found: 26.0 Not Found: true Found: 12345 Example 4Output: Exception in thread "main" java.lang.IllegalArgumentException: radix:555 at java.base/java.util.Scanner.setRadix(Scanner.java:1368) at java.base/java.util.Scanner.nextShort(Scanner.java:2114) at myPackage.ScannerNextShortExample4.main(ScannerNextShortExample4.java:9) Next TopicJava-scanner-radix-method |