Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Core Why and how to use Integer.parseInt() in java

Related Tutorials


 
 

Share on Google+Share on Google+

Why and how to use Integer.parseInt() in java

Advertisement
Integer.parseInt() method is used to convert an string value into integer. JVM reads each element as String and hence we have to convert an entered String value into respective data type

Why and how to use Integer.parseInt() in java.

Integer.parseInt() method is used to convert an string value into integer. JVM reads each element as String and hence we have to convert an entered String value into respective data type. Integer.parseInt() is such a method in Java to convert String input into the Integer. In  java , we know that everything treated as  String format. Now let us suppose we  enter a value in int primitive type, then we have to use int a = Integer.parseInt("String") , because the value we have entered is in integer but it will be treated as as a String so, we have to change the string into integer. Here Integer is a name of a class in java.lang package and parseInt() is a method of Integer class which converts String to integer. If we want to enter a integer in a method or class using keyboard, then we have to use a method parseInt().

Example:

How to use intger.parseInt in java.
public class StringToIntExample {
	public static void main(String[] args) {
		String string = new String("555"); //declare String object
		int i = Integer.parseInt(string); //the Integer parseint method of integer class to convert String into int primitive data type.
		System.out.println("Result is: " + i);
		
         	}	
       }
Here in above example"555"is taking as string and through integet.parseInt() method we are converting it into integer.

Output:
 After compiling and executing the above program.

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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 8, 2013

Related Tutorials

Discuss: Why and how to use Integr.parseInt in java  

Post your Comment


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