xxxxxxxxxx
There are two methods available in java to convert string to integer.
One is
Integer.parseInt() method and another one is
Integer.valueOf() method. Both these methods are static methods
of java.lang.Integer class. Both these methods throw
NumberFormatException if input string is not a valid integer.
xxxxxxxxxx
int result = Integer.parseInt("500");
System.out.println(result) // prints 500 as int
xxxxxxxxxx
String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);
Copy
xxxxxxxxxx
class Scratch{
public static void main(String[] args){
String str = "50";
System.out.println( Integer.parseInt( str )); // Integer.parseInt()
}
}
xxxxxxxxxx
class scratch{
public static void main(String[] args) {
String str = "54";
int num = Integer.parseInt("54");
double doub = Double.parseDouble("54");
}
}
xxxxxxxxxx
String myString = "1234";
int foo = Integer.parseInt(myString);
xxxxxxxxxx
int sample2 = Integer.parseInt("47"); // returns 47
int sample3 = Integer.parseInt("+4"); // returns 4