"1 2"
is not a string representing an int
and therefore, it can not be parsed into an int
.
You need to split the such an input and then you can parse the individual elements into int
values and perform the required arithmetic operation on them e.g.
public class Main {
public static void main(String[] args) {
String input = "1 2";
String[] arr = input.split("\\s+");// Split on whitespace
for (String s : arr) {
System.out.println(s + " + 10 = " + (Integer.parseInt(s) + 10));
}
}
}
Output:
1 + 10 = 11
2 + 10 = 12
solved Java multiple inputs lead to an exception