java.util.Scanner nextShort()

Description :

This java tutorial shows how to use the nextShort method of Scanner class of java.util package. This method returns the short data type which corresponds to the short token on the scanner buffer.

Method Syntax :

There are two overloaded method of Scanner nextShort () java method

java scanner nextShort overload method
Note:
Overloaded methods are those method with same name but with different method signatures


Method 1:

public byte nextShort ()

Method 2:

public byte nextShort(int radix)

Parameter Input :

 

DataType Parameter Description
int radix the base number to scan

 

Method Returns :

This method simply returns the tokens of short data type.

Compatibility Version :

Requires Java 1.5 and up

Exception :

InputMismatchException

– if the next token does not match the Integer regular expression, or is out of range (more than the maximum value or less than the minimum value.

NoSuchElementException

– if input is exhausted and we still invoked the nextShort() method.

IllegalStateException

– if the java scanner is closed and we still invoked the nextShort() method.

Discussion :

The Scanner nextShort() method simply returns short if the token on the scanner buffer can be interpreted or translated of short data type. There are two overloaded method of nextShort method, which accomplishes the same task. The method without a method argument is the same as calling nextShort(radix) which is the default radix of the Scanner object. The radix currently set by the Scanner object is defined using the method useRadix(radix) and can be determine using the radix() method. The radix integer on the method argument nextShort(radix) will override the default radix already defined to the Scanner object.

Make a note that since we are scanning tokens in short data type, there is limit on the maximum and minimum value possible. The Short.MAX_VALUE and Short.MIN_VALUE will show what is the maximum and minimum possible short value than can be evaluated.

Java Code Example :

This java example source code demonstrates the use of nextShort method of Scanner class. Basically the code just prints those tokens of short data type on the input string declared on the Scanner constructor. The radix used on the example below is 8, means to say we have scanned data type in octal base number and then prints the equivalent in decimal format.

package com.javatutorialhq.java.tutorial.scanner;

import java.util.Scanner;

/*
 * Example java source code on the usage of
 * Scanner nextShort() method
 * program to print short tokens
 */

public class ScannerNextShort {

	public static void main(String[] args) {

		// initialize the scanner
		Scanner scan = new Scanner("12 13 -21 -7 1A");
		// tokenize the string on the constructor input
		while(scan.hasNextShort()){
			// printing short tokens
			System.out.println(scan.nextShort(8));
		}
		// closing the scanner object
		scan.close();

	}
}

Sample Output :

Running the nextShort() method example source code of Scanner class will give you the following output

java scanner nextShort method example

java scanner nextShort method example

Exception Scenario :

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextShort(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextShort.main(ScannerNextShort.java:17)

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextShort(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextShort.main(ScannerNextShort.java:23)

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextShort(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextShort.main(ScannerNextShort.java:26)

Similar Method :

  • N/A

Suggested Reading List :

References :