convert a string to int in java:
refer to:alutturi.com/**
* @author lautturi.com
* Java example: converting strings to numbers in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
String number1 = "103";
int result1 = Integer.parseInt(number1); // parse string to integer in java
System.out.println(result1);
String number2 = "103";
Integer result2 = Integer.valueOf(number2);
System.out.println(result2);
String number3 = "103f";
// You will receive an error because there are non-numeric characters in the String.
int result3 = Integer.parseInt(number3);
}
}
output:
103 103 Exception in thread "main" java.lang.NumberFormatException: For input string: "103f" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt