How to convert String to int in Java
Example:
Input: num="1320"
Output: 1320
Approach: 1 : Using parseInt()
Java
public class StringToInt {public static void main(String[] args) {String num = "1320";int numInt = stringToInt(num);System.out.println("Number Integer is " + numInt);}private static int stringToInt(String num) {return Integer.parseInt(num);}}
Approach : 2: Using valueOf()
Java
public class StringToInt {public static void main(String[] args) {String num = "1320";int numInt = stringToInt(num);System.out.println("Number Integer is " + numInt);}private static int stringToInt(String num) {return Integer.valueOf(num);}}
No comments:
Post a Comment