Convert a String into an Integer Data

In this section you will learn to convert a string type of data to integer type data. Converting string to integer and integer to string is a basic task in java programming language because these two type are widely used.

Convert a String into an Integer Data

In this section you will learn to convert a string type of data to integer type data. Converting string to integer and integer to string is a basic task in java programming language because these two type are widely used.

Convert a String into an Integer Data

Convert a String into an Integer Data

In this section you will learn to convert a string type of data to integer type data. Converting string to integer and integer to string is a basic task in java programming language because these two type are widely used. By using Integer.parseInt(string to be convert)  method  convert string to int, this is the best way to convert.

//using integer.ParseInt method
String str="123";
int i=Integer.parseIn("str");
System.out.println("i = "+i);

Integer.parseInt() will throw a NumberFormatExcption,  if the string provided is not a proper number. Similarly you can convert, String to Float, String to Double. Java API provide static method Float.parseFloat(),   Double.parseDouble() for data type conversion.

Example : Code to convert string to int.

public class StringtoInt {
	public static void main(String args[])
	{
		String str="234";
		int i=Integer.parseInt(str);
	    System.out.println(i);
	}

}

Output : After executing the above program

 234