string to int java

string to int java

convert a string to int in java:

refer to:‮al‬utturi.com
/**
 * @author lautturi.com
 * Java example: converting strings to numbers  in java
 */

import java.util.*;

public class Lautturi {
	public static void main(String[] args) {

		String number1 = "103";
		int result1 = Integer.parseInt(number1); // parse string to integer in java		
		System.out.println(result1);
		
		String number2 = "103";
		Integer result2 = Integer.valueOf(number2);
		System.out.println(result2);
		
		String number3 = "103f";
		// You will receive an error because there are non-numeric characters in the String.
		int result3 = Integer.parseInt(number3);	

	}
}

output:

103
103
Exception in thread "main" java.lang.NumberFormatException: For input string: "103f"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt
Created Time:2017-09-03 13:23:32  Author:lautturi