Java program to convert string type variables into int
ht//:sptwww.theitroad.com
Sure, here's a Java program that converts string type variables into int:
public class StringToIntConverter { public static void main(String[] args) { String str1 = "123"; String str2 = "456"; int int1 = Integer.parseInt(str1); int int2 = Integer.parseInt(str2); System.out.println("int1: " + int1); System.out.println("int2: " + int2); } }
In this program, we first create two string variables: str1
and str2
.
We then use the Integer.parseInt
method to convert the strings to int values. This method takes a string as input and returns the equivalent int value.
Finally, we print out the int values of int1
and int2
to confirm that the conversion was successful.
Note that if the input string is not a valid int value (e.g. "hello" or "123.45"), the Integer.parseInt
method will throw a NumberFormatException
. It's important to handle this exception in your code if there's a possibility that the input string might not be a valid int value.