How to convert a String to an int in Java

The Integer Object has a parseInt method that returns an int from a String.

Call the following method:

	
		int output = Integer.parseInt( input );
		 

It is recommended to use this method as Objects can be cached.
Here is an example where the String input is parsed using the method and the output is printed in System.out.

public class IntegerParseInt {

	public static void main(String[] argv){
		
		String input = "5";
		
		int output = Integer.parseInt( input );
	
		System.out.println( "The int value for the String "+ input + " is " + output );
	}
	
}

The output will be:


The int value for the String 5 is 5


References:

java Integer

Share this article:

Recent Comments