//parsing string to int
String numberToParse = "420";
int number = 0;
try{
number = Integer.parseInt(numberToParse);
}catch(NumberFormatException e){
//the string cannot be parsed into a number
//ex Integer.parseInt("d15");
e.printStackTrace();
}
System.out.println(number);
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.
//In java, you can cast to any primitive type by putting (primitiveType)
//before whatever you're casting to.
private static int myInt = (int)myDouble;