Write a Java Program to convert string type variables into int

Java Program to convert string type variables into int

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert string type variables into int
Java Program to convert string type variables into int

Write a Java Program to convert string type variables into int

PROGRAM: Java Program to convert string type variables into int

/*Java Program to convert string type variables into int*/

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

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

// create string variables
String str1 = “23”;
String str2 = “4566”;

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

// print int values
System.out.println(num1); // 23
System.out.println(num2); // 4566
}
}

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

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

// create string variables
String str1 = “643”;
String str2 = “1312”;

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

// print int values
System.out.println(num1); // 643
System.out.println(num2); // 1312
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *