Java - Short parseShort() 方法



Java Short parseShort() 方法用於將給定的字串值解析(轉換)為 Short 物件。此方法將給定的字串解析為帶符號的十進位制短整型。

提供的字串中的字元必須全部為十進位制數字,除了第一個字元,它可以是 ASCII 減號 ('-') 或 ASCII 加號 ('+') ('u002B'),分別表示負值或正值。

語法

以下是 Java Short parseShort() 方法的語法:-

public static short parseShort(String s) throws NumberFormatException
or,
public static short parseShort(String s, int radix) throwsNumberFormatException

引數

  • s - 這是一個包含要解析的短整型表示形式的字串。

  • radix - 這是解析 s 時使用的基數。

返回值

此方法返回由字串引數在指定基數中表示的短整型值。

從包含正值的字串獲取短整型值示例

我們可以使用 parseShort() 方法傳遞正值。結果將是給定字串值的等效短整型值。

以下示例顯示了 Java Short parseShort() 方法的使用。這裡我們透過建立一個 Short 物件 's' 並返回字串變數 'str' 表示的短整型值。

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      Short s = new Short("6");
      
      // returns the short value represented by the string argument
      String str = "50";
      short retval = s.parseShort(str);
      System.out.println("Value = " + retval);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:-

Value = 50

從包含正值的字串獲取短整型值示例

在下面給出的示例中,聲明瞭三個字串變數,即 's1'、's2'、's3',併為其分配了值。透過使用 parseShort() 方法,傳遞了與字串相對應的短整型值以及一些操作,如連線、加法、乘法和除法。然後返回其對應的短整型值:-

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      String s1 = "8";
      String s2 = "13";
      String s3 = "4";
      
      // concatenating two strings instead of adding it
      Short d1 = Short.parseShort(s1 + s2 + s3);
      System.out.println("The sum is =" + d1);
      System.out.println("The sum is =" + (Short.parseShort(s1) + Short.parseShort(s2) + Short.parseShort(s3)));
      System.out.println("The Product is =" + (Short.parseShort(s1) * Short.parseShort(s2) * Short.parseShort(s3)));
      System.out.println("The division is =" + (Short.parseShort(s2) / Short.parseShort(s3)));
      System.out.println("The division =" + (Short.parseShort(s1) / Short.parseShort(s2)));
   }
}

輸出

以下是上述程式碼的輸出:-

The sum is =8134
The sum is =25
The Product is =416
The division is =3
The division =0

從包含無效值的字串獲取短整型值時遇到異常示例

當將字母數字值作為引數傳遞給此方法時,它會返回 NumberFormat Exception。

在下面的程式碼中,傳遞的字串值不包含可解析的值,因此它會丟擲異常錯誤:-

package com.tutorialspoint;

import java.util.Scanner;

public class ShortDemo {
   public static void main(String[] args) {
      String s = "Deepak@21";
      Short d = Short.parseShort(s);
      System.out.println("The equivalent short value is = " + d);
   }
}

NumberFormatException

上述程式碼丟擲異常錯誤,如下面的輸出所示:-

Exception in thread "main" java.lang.NumberFormatException: For input string: "Deepak@21"
      at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
      at java.base/java.lang.Integer.parseInt(Integer.java:668)
      at java.base/java.lang.Short.parseShort(Short.java:137)
      at java.base/java.lang.Short.parseShort(Short.java:163)
      at com.tutorialspoint.ShortDemo.main(ShortDemo.java:6)

從具有給定基數的字串獲取短整型值示例

以下是一個示例,其中檢索了指定字串(帶基數)的帶符號十進位制短整型值:-

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      String str = "1000";
      
      // returns signed decimal short value of string
      short shortValue = Short.parseShort(str);  
      
      // prints signed decimalshort value
      System.out.println("Signed decimal short value for given String is = " + shortValue);  	 
      
      // returns the string argument as a signed short in the radix
      shortValue = Short.parseShort(str,2);
      System.out.println("Signed decimal short value for specified String with radix 2 is = " + shortValue);
      
      // returns the string argument as a signed short in the radix
      shortValue = Short.parseShort("11",8);
      System.out.println("Signed decimal short value for specified String with radix 8 is = " + shortValue);
   }
}   

輸出

執行上述程式碼時,我們將獲得以下輸出:-

Signed decimal short value for given String is = 1000
Signed decimal short value for specified String with radix 2 is = 8
Signed decimal short value for specified String with radix 8 is = 9
java_lang_short.htm
廣告