Scanner useRadix(int) in Java

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

Syntax:

Scanner java.util.Scanner.useRadix(int radix)

This method takes one argument. This method sets this scanner's default radix to the specified radix.

Parameters: One parameter is required for this method.

radix: The radix to use when scanning numbers.

Returns: this scanner.

Throws:

IllegalArgumentException - if the radix is out of range

Approach 1: When no exception

Java

import java.util.Scanner;

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

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

        int radix = Character.MIN_RADIX;
        System.out.println(scanner.useRadix(radix));
        scanner.close();
    }
}

Output:

java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\x{2c}][decimal separator=\x{2e}][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]



Approach 2: IllegalArgumentException 

Java

import java.util.Scanner;

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

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

        int radix = Character.MIN_RADIX - 1;
        System.out.println(scanner.useRadix(radix));
        scanner.close();
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: radix:1 at java.base/java.util.Scanner.useRadix(Scanner.java:1353)


No comments:

Post a Comment