Convert String to Short in Java with Examples - Java 147

Convert String to Short in Java with Examples – Java 147

Convert String to Short in Java with Examples

對包含 short 的字串進行數學運算時,通常會使用轉換成 short ,從文本字段或文本區域讀取資料時,輸入的資料都會作為字串接收,將字串轉換為 short ,可以使用 decode 、 Apache Commons Lang 等方法進行操作, Convert a String to a Short in Java 任務是將該字串轉換為短整數,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- conversion
       |                   +- string
       |                       +- StringToShortTest.java   

單元測試

Convert a String to Short in Java 提供字串轉換成短整數等操作。

decode

Convert a String to Short in Java 使用 Short.decode 轉換成短整數。

	@Test
	public void decode() {
		String value = "155";
		short result = Short.decode(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "-70";
		result = Short.decode(value);
		System.out.println(result);
		assertEquals(-70, result);
	}
155
-70

decodeThrowException

Convert a String to Short in Java 使用 Short.decode 轉換成短整數,輸入不合格式的字串,會拋出例外。

	@Test(expected = NumberFormatException.class)
	public void decodeThrowException() {
		String value = "155";
		short result = Short.decode(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "assertj-155";
		result = Short.decode(value);
		System.out.println(result);
	}
155
java.lang.NumberFormatException: For input string: "assertj-155"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.valueOf(Integer.java:740)
	at java.lang.Integer.decode(Integer.java:1197)
	at java.lang.Short.decode(Short.java:282)
	at org.ruoxue.java_147.string.StringToShortWithExamplesTest.decodeThrowException(StringToShortWithExamplesTest.java:31)

constructor

Convert a String to Short in Java 使用 Short 建構子,傳入參數字串,建立一個新的 Short 物件。

	@Test
	public void constructor() {
		String value = "155";
		short result = new Short(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "-70";
		result = new Short(value);
		System.out.println(result);
		assertEquals(-70, result);
	}
155
-70

constructorThrowException

使用 Short 建構子,傳入參數字串,建立一個新的 Short 物件,輸入不合格式的字串,會拋出例外。

	@Test(expected = NumberFormatException.class)
	public void constructorThrowException() {
		String value = "155";
		short result = new Short(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "assertj-155";
		result = new Short(value);
		System.out.println(result);
	}
155
java.lang.NumberFormatException: For input string: "assertj-155"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Short.parseShort(Short.java:118)
	at java.lang.Short.<init>(Short.java:321)
	at org.ruoxue.java_147.string.StringToShortWithExamplesTest.constructorThrowException(StringToShortWithExamplesTest.java:56)

NumberUtils_toShort

使用 Apache Commons Lang – NumberUtils 轉換成短整數。

	@Test
	public void NumberUtils_toShort() {
		String value = "155";
		short result = NumberUtils.toShort(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "-70";
		result = NumberUtils.toShort(value);
		System.out.println(result);
		assertEquals(-70, result);

		value = "70";
		result = NumberUtils.toShort(value, (short) 0);
		System.out.println(result);
		assertEquals(70, result);

		value = "assertj-155";
		result = NumberUtils.toShort(value, (short) 0);
		System.out.println(result);
		assertEquals(0, result);
	}
155
-70
70
0

StringToShortWithExamplesTest.java

Java String to Short Examples 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.conversion.string;

import static org.junit.Assert.*;

import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;

public class StringToShortWithExamplesTest {

	@Test
	public void decode() {
		String value = "155";
		short result = Short.decode(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "-70";
		result = Short.decode(value);
		System.out.println(result);
		assertEquals(-70, result);
	}

	@Test(expected = NumberFormatException.class)
	public void decodeThrowException() {
		String value = "155";
		short result = Short.decode(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "assertj-155";
		result = Short.decode(value);
		System.out.println(result);
	}

	@Test
	public void constructor() {
		String value = "155";
		short result = new Short(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "-70";
		result = new Short(value);
		System.out.println(result);
		assertEquals(-70, result);
	}

	@Test(expected = NumberFormatException.class)
	public void constructorThrowException() {
		String value = "155";
		short result = new Short(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "assertj-155";
		result = new Short(value);
		System.out.println(result);
	}

	@Test
	public void NumberUtils_toShort() {
		String value = "155";
		short result = NumberUtils.toShort(value);
		System.out.println(result);
		assertEquals(155, result);

		value = "-70";
		result = NumberUtils.toShort(value);
		System.out.println(result);
		assertEquals(-70, result);

		value = "70";
		result = NumberUtils.toShort(value, (short) 0);
		System.out.println(result);
		assertEquals(70, result);

		value = "assertj-155";
		result = NumberUtils.toShort(value, (short) 0);
		System.out.println(result);
		assertEquals(0, result);
	}
}

心得分享

Convert a String to a Short in Java with Examples 將 String 實例轉換為 short 或 Short 實例的多種方法, 除了使用 Java 所提供的 decode 、 constructor  之外,也有其他選擇使用外部程式庫來轉換成 short ,像是 Apache Commons Lang 等, Java String to Short Examples 提供了幾種常見方法的操作範例。

發佈留言