Java Program to convert string type variables into int

Example 1: Java Program to Convert string to int using parseInt()

class StringToInt {
  public static void main(String[] args) {

    // create two string variables
    String str1 = "43";
    String str2 = "5456";

    // convert string to int using parseInt()
    int num1 = Integer.parseInt(str1);
    int num2 = Integer.parseInt(str2);

    // print int values
    System.out.println(num1);    
    System.out.println(num2);    
  }
}
Output
43
5456

Example 2: Java Program to Convert string to int using valueOf()

class StringToInt {
  public static void main(String[] args) {

    // create two string variables
    String str1 = "364";
    String str2 ="2131";

    // convert String to int using valueOf()
    int num1 = Integer.valueOf(str1);
    int num2 = Integer.valueOf(str2);

    // print int values
    System.out.println(num1);    
    System.out.println(num2);      }
}
Output
364
2131

Leave a Reply