O método hasNextShort() da classe java.util.Scanner retorna verdadeiro se o próximo token na entrada deste scanner pode ser assumido como um valor curto da raiz fornecida. O scanner não avança além de nenhuma entrada. Caso nenhuma raiz seja passada como parâmetro, a função interpreta a raiz como raiz padrão e funciona de acordo.

Sintaxe:

public boolean hasNextShort(int radix)
            or
public boolean hasNextShort()

Parâmetros: A função aceita uma única raiz de parâmetro que não é obrigatória. Ele especifica a raiz usada para interpretar o token como um valor curto.

Valor de retorno: esta função retorna verdadeiro se e somente se o próximo token deste scanner for um valor curto válido na raiz padrão.

Exceções : a função lança IllegalStateException se este scanner for fechado.

Os programas abaixo ilustram a função acima:

Programa 1:

// Java program to illustrate the
// hasNextShort() method of Scanner class in Java
// with parameter
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "gfg 2 geeks!";
  
        // new scanner with the
        // specified String Object
        Scanner scanner = new Scanner(s);
  
        // use US locale to interpret shorts in a string
        scanner.useLocale(Locale.US);
  
        // iterate till end
        while (scanner.hasNext()) {
  
            // check if the scanner's
            // next token is a short with a radix 3
            System.out.print("" + scanner.hasNextShort(3));
  
            // print what is scanned
            System.out.print(" -> " + scanner.next() + "\n");
        }
  
        // close the scanner
        scanner.close();
    }
}
Saída:
false -> gfg
verdadeiro -> 2
false -> geeks!

Programa 2:

// Java program to illustrate the
// hasNextShort() method of Scanner class in Java
// without parameter
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "gfg 2 geeks!";
  
        // new scanner with the
        // specified String Object
        Scanner scanner = new Scanner(s);
  
        // use US locale to interpret shorts in a string
        scanner.useLocale(Locale.US);
  
        // iterate till end
        while (scanner.hasNext()) {
  
            // check if the scanner's
            // next token is a short with the default radix
            System.out.print("" + scanner.hasNextShort());
  
            // print what is scanned
            System.out.print(" -> " + scanner.next() + "\n");
        }
  
        // close the scanner
        scanner.close();
    }
}
Saída:
false -> gfg
verdadeiro -> 2
false -> geeks!

Programa 3: Programa para demonstrar IllegalStateException

// Java program to illustrate the
// hasNextShort() method of Scanner class in Java
// Exception case
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
            String s = "gfg 2 geeks!";
  
            // new scanner with the
            // specified String Object
            Scanner scanner = new Scanner(s);
  
            // use US locale to interpret shorts in a string
            scanner.useLocale(Locale.US);
  
            scanner.close();
  
            // iterate till end
            while (scanner.hasNext()) {
  
                // check if the scanner's
                // next token is a short with the default radix
                System.out.print("" + scanner.hasNextShort());
  
                // print what is scanned
                System.out.print(" -> " + scanner.next() + "\n");
            }
  
            // close the scanner
            scanner.close();
        }
        catch (IllegalStateException e) {
            System.out.println("Exception: " + e);
        }
    }
}
Saída:
Exceção: java.lang.IllegalStateException: Scanner fechado

Referência: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextShort()