Create a simple addition calculator in Java. The program should prompt the user to enter 2 integers, then adds the numbers and prints the result. Make sure the program includes appropriate exception handling in case the user does not enter appropriate integer values.
import java.util.InputMismatchException; import java.util.Scanner; public class AddIntegers { public static void main(String[] args) { Scanner in = new Scanner(System.in); try { System.out.print("Enter first integer: "); int n1 = in.nextInt(); System.out.print("Enter second integer: "); int n2 = in.nextInt(); System.out.println("sum of " + n1 + " and " + n2 + " is " + (n1 + n2)); } catch (InputMismatchException e) { System.out.println("You must enter integers only.."); } } }
Get Answers For Free
Most questions answered within 1 hours.