Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaScanner hasNextShort() method in Java with Examples

Scanner hasNextShort() method in Java with Examples

The hasNextShort() method of java.util.Scanner class returns true if the next token in this scanner’s input can be assumed as a short value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix and functions accordingly. Syntax: 

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

Parameters: The function accepts a single parameter radix which is not a mandatory one. It specifies the radix used to interpret the token as a short value. Return Value: This function returns true if and only if this scanner’s next token is a valid short value in the default radix. Exceptions: The function throws IllegalStateException if this scanner is closed. Below programs illustrate the above function: Program 1: 

Java




// 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();
    }
}


Output:

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

Program 2: 

Java




// 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();
    }
}


Output:

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

Program 3: Program to demonstrate IllegalStateException 

Java




// 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);
        }
    }
}


Output:

Exception: java.lang.IllegalStateException: Scanner closed

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments