Convert String to Int in Java with Examples - Java 147

Convert String to Int in Java with Examples – Java 147

Convert String to Int in Java with Examples

將 String 轉換為 int 或 Integer 是 Java 中常見的操作,可以使用 decode 、 Apache Commons Lang 等方法進行操作, Convert a String to an Int in Java 任務是將該字串轉換為整數,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

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

decode

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

	@Test
	public void decode() {
		String value = "147";
		int result = Integer.decode(value);
		System.out.println(result);
		assertEquals(147, result);

		value = "-20";
		result = Integer.decode(value);
		System.out.println(result);
		assertEquals(-20, result);
	}
147
-20

decodeThrowException

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

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

		value = "java-147";
		result = Integer.decode(value);
		System.out.println(result);
	}
147
java.lang.NumberFormatException: For input string: "java-147"
	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 org.ruoxue.java_147.string.StringToIntWithExamplesTest.decodeThrowException(StringToIntWithExamplesTest.java:31)

constructor

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

	@Test
	public void constructor() {
		String value = "147";
		int result = new Integer(value);
		System.out.println(result);
		assertEquals(147, result);

		value = "-20";
		result = new Integer(value);
		System.out.println(result);
		assertEquals(-20, result);
	}
147
-20

constructorThrowException

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

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

		value = "java-147";
		result = new Integer(value);
		System.out.println(result);
	}
147	
java.lang.NumberFormatException: For input string: "java-147"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.<init>(Integer.java:867)
	at org.ruoxue.java_147.string.StringToIntWithExamplesTest.constructorThrowException(StringToIntWithExamplesTest.java:56)

NumberUtils_toInt

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

	@Test
	public void NumberUtils_toInt() {
		String value = "147";
		int result = NumberUtils.toInt(value);
		System.out.println(result);
		assertEquals(147, result);

		value = "-20";
		result = NumberUtils.toInt(value);
		System.out.println(result);
		assertEquals(-20, result);

		value = "20";
		result = NumberUtils.toInt(value, 0);
		System.out.println(result);
		assertEquals(20, result);

		value = "java-147";
		result = NumberUtils.toInt(value, 0);
		System.out.println(result);
		assertEquals(0, result);
	}
147
-20
20
0

StringToIntWithExamplesTest.java

Java String to Int 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 StringToIntWithExamplesTest {

	@Test
	public void decode() {
		String value = "147";
		int result = Integer.decode(value);
		System.out.println(result);
		assertEquals(147, result);

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

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

		value = "java-147";
		result = Integer.decode(value);
		System.out.println(result);
	}

	@Test
	public void constructor() {
		String value = "147";
		int result = new Integer(value);
		System.out.println(result);
		assertEquals(147, result);

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

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

		value = "java-147";
		result = new Integer(value);
		System.out.println(result);
	}

	@Test
	public void NumberUtils_toInt() {
		String value = "147";
		int result = NumberUtils.toInt(value);
		System.out.println(result);
		assertEquals(147, result);

		value = "-20";
		result = NumberUtils.toInt(value);
		System.out.println(result);
		assertEquals(-20, result);

		value = "20";
		result = NumberUtils.toInt(value, 0);
		System.out.println(result);
		assertEquals(20, result);

		value = "java-147";
		result = NumberUtils.toInt(value, 0);
		System.out.println(result);
		assertEquals(0, result);
	}
}

心得分享

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

發佈留言