int result = Integer.parseInt("500");
System.out.println(result) // prints 500 as int
int result = Integer.parseInt(number);
String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);
Copy
int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e)
{
foo = 0;
}
int i=Integer.parseInt("200");
String example = "1";
int converted = Integer.parseInt(example);
int sample2 = Integer.parseInt("47"); // returns 47
int sample3 = Integer.parseInt("+4"); // returns 4
String toBeParsed = "15";
int parsedString = Integer.parseInt(String.valueOf(toBeParsed));
/*this makes the integer value of parsedString the number held
in the string toBeParsed*/
/*Side note: you will have to import java.lang.String to be
able to use Integer.parseInt() and String.valeOf() */
//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);
//String to int , scanner, NumberFormatException,
Scanner scanner = new Scanner(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);
}
}
String myString = "1234";
int foo = Integer.parseInt(myString);
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)
string = "some string"
string = int(string)
int num = atoi(a);
int myInt = Interger.parseInt(String);
public int stringToInt(String str) {
// deleting everything that is not numeric
return Integer.parseInt(str.replaceAll("[^0-9.]",""));
}
String str = "ab";
char[] ch = str.toCharArray();
System.out.println("position 0 = " + (int)ch[0]);
stoi(str);
stoi(str);