Java is an object orientied programming languege. In Java you can use Integer.parseInt() or Integer.valueOf() functions to convert String value to an int value. In any case String is not a convertible then NumberFormatException will occur. #1. Using Integer.parseInt() Integer.parseInt() converts string value and returns an int primitive as result. For below example, first, we store 5 as a string value to id variable. Then convert it to integer and save to result variable.
1 2 3 4 5 6 7 8 9 | class ConvertStringToInt1 { public static void main(String args[]){ String id = "5"; int result = Integer.parseInt(id); System.out.println(result); } } |
Save the above content in ConvsertStringToInt1.java file and compile and run this program. Compile: javac ConvertStringToInt1.java Run: java ConvertStringToInt1 5 #2. Using Integer.valueOf() Integer.valueOf() also…