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
String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);
int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e)
{
foo = 0;
}
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);
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]);