Home Java Java-conversion Convert a String into an Integer Data
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert a String into an Integer Data

Advertisement
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. 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 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 22, 2013

Ask Questions?    Discuss: Convert a String into an Integer Data  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com