Java Short 類的 parseShort() 方法用於將字符串參數解析為有符號十進製短整數。
用法
static short parseShort(String x)throws NumberFormatException
參數
x:包含要解析的短表示的字符串。字符串中的所有字符都必須是十進製數字。
返回值
它返回由十進製參數表示的短值。
異常
如果字符串不包含可解析的 short,則拋出 NumberFormatException。
例子1
public class ShortParseShortExample1 {
public static void main(String[] args) {
//with Positive String Value
String x = "100";
// it returns signed decimal short value of string
short sValue = Short.parseShort(x);
// printing signed parseShort value
System.out.println("String "+x +" , parse value into parseShort =" + sValue);
}
}
輸出:
String 100 , parse value into parseShort =100
例子2
public class ShortParseShortExample2 {
public static void main(String[] args) {
//with Negative String Value
String x = "-500";
// it returns signed decimal short value of string
short sValue = Short.parseShort(x);
// printing signed parseShort value
System.out.println("String "+x +" , parse value into parseShort =" + sValue);
}
}
輸出:
String -500 , parse value into parseShort =-500
例子3
public class ShortParseShortExample3 {
public static void main(String[] args) {
String x = "Hello";
// in this case, it will throw exception because String doesn't contain parsable value.
short sValue = Short.parseShort(x);
// printing signed parseShort value
System.out.println("String "+x +" , parse value into parseShort =" + sValue);
}
}
輸出:
NumberFormatException
短片:

Java Short parseShort(String x, int radix) 方法
java Short 類的 parseShort(String x, int radix ) 方法用於將字符串參數解析為第二個參數指定的基數中的有符號short。
用法
static short parseShort(String x, int radix) throws NumberFormatException
參數
x:包含要解析的短的字符串。
radix:解析包含短值的字符串時要使用的基數。
返回值
它返回由指定基數中的字符串參數表示的 short。
異常
如果出現以下情況,它會拋出 NumberFormatException:
- 第一個參數為空或長度為零的字符串
- 字符串的任何字符都不是數字。
- 基數小於 Character.MIN_RADIX 或大於 Character.MAX_RADIX
- 字符串表示的值不是 short 類型的值
示例 4
public class ShortParseShortExample4 {
public static void main(String[] args) {
String x="100";
int radix=2;
// it parse string as a signed short in the radix
short shortValue = Short.parseShort(x,radix);
System.out.println(" String "+x+" parsed specified by radix "+radix + " , into signed decimal short = "+ shortValue);
}
}
輸出:
String 100 parsed specified by radix 2 , into signed decimal short = 4
例 5
public class ShortParseShortExample5{
public static void main(String[] args) {
String x="abc"; //Non parsable value. throw exception
int radix=2;
// it parse string as a signed short in the radix
short shortValue = Short.parseShort(x,radix);
System.out.println(" String "+x+" parsed specified by radix "+radix + " , into signed decimal short = "+ shortValue);
}
}
輸出:
NumberFormatException
短片:

例 6
字符串長度為零
public class ShortParseShortExample6 {
public static void main(String[] args) {
String x=" "; //String length is Zero . throw exception
int radix=2;
// it parse string as a signed short in the radix
short shortValue = Short.parseShort(x,radix);
System.out.println(" String "+x+" parsed specified by radix "+radix + " , into signed decimal short = "+ shortValue);
}
}
輸出:
NumberFormatException
短片:

例 7
public class ShortParseShortExample7 {
public static void main(String[] args) {
// it parse string as a signed short in the radix
short shortValue = Short.parseShort("55", 6);
System.out.println(" String parsed specified by radix , into signed decimal short = "+ shortValue);
}
}
輸出:
String parsed specified by radix into signed decimal short = 35
相關用法
- Java Short hashCode()用法及代碼示例
- Java Short shortValue()用法及代碼示例
- Java Short compare()用法及代碼示例
- Java Short equals()用法及代碼示例
- Java Short decode()用法及代碼示例
- Java Short valueOf()用法及代碼示例
- Java Short longValue()用法及代碼示例
- Java Short doubleValue()用法及代碼示例
- Java Short toString()用法及代碼示例
- Java Short byteValue()用法及代碼示例
- Java Short compareTo()用法及代碼示例
- Java Short reverseBytes()用法及代碼示例
- Java Short floatValue()用法及代碼示例
- Java Short intValue()用法及代碼示例
- Java Short compareUnsigned()用法及代碼示例
- Java ShortBuffer rewind()用法及代碼示例
- Java Guava Shorts.min()用法及代碼示例
- Java Guava Shorts.max()用法及代碼示例
- Java ShortBuffer hasArray()用法及代碼示例
- Java ShortBuffer allocate()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Short parseShort() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。