Home Java Java-conversion Convert String to long

Related Tutorials


 
 

Share on Google+Share on Google+

Convert String to long

Advertisement
This page discusses - Convert String to long

Convert String to long

     

In this section, we will learn how to convert String to long. The following program provides you the functionality to convert String to long.

Code Description:

In this program we have taken a String as "String s = 20000" as shown below. To convert this String to long we have used "parseLong()" method in a try and catch block to catch the exception if occurs. Therefore we get the following output of the program as shown below.

Here is the code of the program:

public class StrToLong {
  public static void main (String[] args) {
 String s = "20000";
 try {
 long l = Long.parseLong(s.trim());
 System.out.println("long l = " + l);
  catch (NumberFormatException e) {
 System.out.println("NumberFormatException: " + e.getMessage());
  }
  }
 }

Output of the program:

C:\unique>javac StrToLong.java

C:\unique>java StrToLong
long l = 20000

C:\unique>

Download this example.

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: April 17, 2011

Related Tutorials

Discuss: Convert String to long  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
nice
November 9, 2011
thx

good
DMCA.com