Short.parseShort() in Java

Short.parseShort(): This method is available in java.lang.Short class of Java.

Approach 1: When the method takes one argument.

Syntax:

short java.lang.Short.parseShort(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method parses the string argument as a signed decimal short. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u005Cu002D') to indicate a negative value or an ASCII plus sign '+'('\u005Cu002B') to indicate a positive value.

Parameters: One parameter is required for this method.

s: a String containing the short representation to be parsed.

Returns: the short value represented by the argument in decimal.

Throws:

NumberFormatException - If the string does not contain a parsable short.

Java

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

        String s = "12";
        System.out.println(Short.parseShort(s));
    }
}

Output:

12


Approach 1.1: NumberFormatException

Java

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

        String s = "hello";
        System.out.println(Short.parseShort(s));
    }
}


Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Short.parseShort(Short.java:130) at java.base/java.lang.Short.parseShort(Short.java:156)


Approach 2: When the method takes two arguments.

Syntax:

short java.lang.Short.parseShort(String s, int radix) throws NumberFormatException

This method takes two arguments one of type String and another of type int as its parameters. This method parses the string argument as a signed short in the radix specified by the second argument. The characters in the string must all be digits, of the specified radix except that the first character may be an ASCII minus sign  '-'('\u005Cu002D') to indicate a negative value or an ASCII plus sign '+' ('\u005Cu002B') to indicate a positive value. The resulting short value is returned.

Parameters: Two parameters is required for this method.

s: the String containing the short representation to be parsed.

radix: the radix to be used while parsings.

Returns: the short represented by the string argument in the specified radix.

Throws:

NumberFormatException - If the String does not contain a parsable short.

Java

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

        String s = "101";
        int radix = 2;
        System.out.println(Short.parseShort(s, radix));
    }
}

Output:

5


Approach 2.1: NumberFormatException

Java

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

        String s = "hello";
        int radix = 2;
        System.out.println(Short.parseShort(s, radix));
    }
}


Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" under radix 2 at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Short.parseShort(Short.java:130)


No comments:

Post a Comment