//parsing string to intString 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);
//String to int , scanner, NumberFormatException, Scanner scanner =newScanner(System.in);while(true){String input = scanner.next();try{int a =Integer.parseInt(input);if(a ==0){break;}System.out.println(Integer.parseInt(input)*10);}catch(NumberFormatException e){System.out.println("Invalid user input: "+ input);}}
There are two methods available in java toconvert string tointeger.One is
Integer.parseInt() method and another one is
Integer.valueOf()method. Both these methods are static methods
of java.lang.Integerclass. Both these methods throwNumberFormatExceptionif input string is not a valid integer.
num ='10'
# check and print type num variable
print(type(num))
# convert the num into string
converted_num =int(num)
# print type of converted_num
print(type(converted_num))
# We can check by doing some mathematical operations
print(converted_num +20)
<script>
function convertStoI(){var a ="100";var b =parseInt(a);
document.write("Integer value is"+ b);var d =parseInt("3 11 43");
document.write("</br>");
document.write('Integer value is ' + d);}convertStoI();</script>