Escáner hasNextShort() método en Java con ejemplos

El método hasNextShort() de la clase java.util.Scanner devuelve verdadero si el siguiente token en la entrada de este escáner se puede asumir como un valor corto de la base dada. El escáner no avanza más allá de ninguna entrada. En caso de que no se pase base como parámetro, la función interpreta la base como base predeterminada y funciona en consecuencia.

Sintaxis:

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

Parámetros: La función acepta un solo parámetro base que no es obligatorio. Especifica la raíz utilizada para interpretar el token como un valor corto.

Valor devuelto: esta función devuelve verdadero si y solo si el próximo token de este escáner es un valor corto válido en la raíz predeterminada.

Excepciones : la función lanza IllegalStateException si este escáner está cerrado.

Los siguientes programas ilustran la función anterior:

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();
    }
}
Producción:

false -> gfg
true -> 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();
    }
}
Producción:

false -> gfg
true -> 2
false -> geeks!

Programa 3: Programa para demostrar 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);
        }
    }
}
Producción:

Exception: java.lang.IllegalStateException: Scanner closed

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

Publicación traducida automáticamente

Artículo escrito por gopaldave y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *