Scanner nextShort(int) in Java

nextShort(int): This method is available in java.util.Scanner class of Java.

Syntax:

short java.util.Scanner.nextShort(int radix)

This method takes one argument. This method scans the next token of the input as a short.

Parameters: One parameter is required for this method.

radix: the radix is used to interpret the token as a short value.

Returns: the short scanned from the input.

Throws:

1. InputMismatchException - if the next token does not match the Integer regular expression, or is out of range.

2. NoSuchElementException - if input is exhausted.

3. IllegalStateException - if this scanner is closed.

4. IllegalArgumentException - if the radix is out of range

Approach 1: When no exception

Java

import java.util.Scanner;

public class ScannernextShort2 {
    public static void main(String[] args) {

        String source = "11100 Hello World";
        Scanner scanner = new Scanner(source);

        int radix = Character.MIN_RADIX;
        System.out.println(scanner.nextShort(radix));

        scanner.close();
    }
}

Output:

28


Approach 2: InputMismatchException 

Java

import java.util.Scanner;

public class ScannernextShort2 {
    public static void main(String[] args) {

        String source = "Hello World";
        Scanner scanner = new Scanner(source);

        int radix = Character.MIN_RADIX;
        System.out.println(scanner.nextShort(radix));

        scanner.close();
    }
}

Output:

Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextShort(Scanner.java:2118)



Approach 3: NoSuchElementException

Java

import java.util.Scanner;

public class ScannernextShort2 {
    public static void main(String[] args) {

        String source = "";
        Scanner scanner = new Scanner(source);

        int radix = Character.MIN_RADIX;
        System.out.println(scanner.nextShort(radix));

        scanner.close();
    }
}

Output:

Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextShort(Scanner.java:2118)



Approach 4: IllegalStateException 

Java

import java.util.Scanner;

public class ScannernextShort2 {
    public static void main(String[] args) {

        String source = "11100 Hello World";
        Scanner scanner = new Scanner(source);

        int radix = Character.MIN_RADIX;

        scanner.close();
        System.out.println(scanner.nextShort(radix));

    }
}

Output:

Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150) at java.base/java.util.Scanner.next(Scanner.java:1573) at java.base/java.util.Scanner.nextShort(Scanner.java:2118)



Approach 5: IllegalArgumentException

Java

import java.util.Scanner;

public class ScannernextShort2 {
    public static void main(String[] args) {

        String source = "11100 Hello World";
        Scanner scanner = new Scanner(source);

        int radix = Character.MIN_RADIX - 1;
        System.out.println(scanner.nextShort(radix));

        scanner.close();
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: radix:1 at java.base/java.util.Scanner.setRadix(Scanner.java:1368) at java.base/java.util.Scanner.nextShort(Scanner.java:2114)


No comments:

Post a Comment