java.lang.Short parseShort(String s)
Description
The Short.parseShort(String s) java method is used primarily in parsing a String method argument into a Short object. The Short object is a wrapper class for the short primitive data type of java API. Even though the conversion of String to Short object is a basic stuff, I am suggesting that you should have a mastery on it. In my years of java programming, I have encountered so many instances that I have to convert from String object to another data type. The Short.parseShort method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Short.
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 ‘-‘ (‘\u002D’) to indicate a negative value or an ASCII plus sign ‘+’ (‘\u002B’) to indicate a positive value. The resulting short value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseShort(java.lang.String, int) method.
Make a note that the parseShort method of Short class is static thus it should be accessed statically. Mean to say the we would be calling this method in this format:
Short.parseShort(method args)
Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseShort method non statically.
Important Notes:
- This method throws NumberFormatException if the string method argument does not contain a parsable short.
Method Syntax
public static short parseShort(String s) throws NumberFormatException
Method Argument
Data Type | Parameter | Description |
---|---|---|
String | s | a String containing the short representation to be parsed |
Method Returns
The parseShort(String s) method of Short class returns the short value represented by the argument in decimal.
Compatibility
Requires Java 1.1 and up
Java Short parseShort(String s) Example
Below is a simple java example on the usage of parseShort(String s) method of Short class.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * This example source code demonstrates the use of * parseShort(String s) method of Short class */ public class ParseShortStringExample { public static void main(String[] args) { // Ask user input System.out.print("Enter height of rectangle:"); // declare the scanner object Scanner scan = new Scanner(System.in); // use scanner to get height of a rectangle String height = scan.nextLine(); // ask for another input System.out.print("Enter base of rectangle:"); //use scanner to get base of a rectangle String base = scan.nextLine(); // close the scanner object scan.close(); // convert the String input to Short Short baseShort = Short.parseShort(base); Short heightShort = Short.parseShort(height); // calculate the area of rectangle int areaRectangle = baseShort * heightShort; System.out.println("Area of the rectangle is "+areaRectangle); } }