You can use the function Integer.parseInt(String)
which will parse the string argument as a signed decimal integer.:
void calc() {
System.out.println(Integer.parseInt(line1) + Integer.parseInt(line2));
}
Note: This will throw an exception if one of the strings cannot be converted into an integer. You can handle this exception by using a try-catch
block:
try {
// operations
} catch (NumberFormatException e) {
e.printStackTrace();
}