Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Convert String to primitive int example

  1. /*
  2.  * Convert String to primitive int example. Program to convert Java
  3.  * String object to primitive int data type.
  4.  */
  5.  
  6. public class StringToInt {
  7.  
  8.  public static void main(String args[]){
  9.  
  10.   //new String object
  11.   String str = new String("342");
  12.  
  13.   /*
  14.    * To convert String to primitive int data type, use
  15.    * parseInt(String) static method of Integer class
  16.    */
  17.  
  18.   int i = Integer.parseInt(str);
  19.  
  20.   //this throws NumberFormatException for invalid number
  21.  
  22.   System.out.println("String converted to primitive int: " + i);
  23.  
  24.  }
  25.  
  26. }
Your rating: None Average: 5 (1 vote)
Share this

Post new comment