Guest User

Untitled

a guest
Nov 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.*;
  3.  
  4. public class ExceptionHandling {
  5. public static void main(String[] args) throws InputMismatchException {
  6.  
  7. Scanner input = new Scanner(System.in);
  8. boolean continueInput = true;
  9.  
  10. // Prompt user to enter two integers
  11. do {
  12. try {
  13. System.out.print("Enter two integers: ");
  14. int number1 = input.nextInt();
  15. int number2 = input.nextInt();
  16. int result = number1 + number2;
  17. System.out.println(number1 + " + " + number2 + " = " + result);
  18.  
  19. continueInput = false;
  20. }
  21. catch (InputMismatchException ex) {
  22. System.out.println("Exception: Invalid entry! Please re-enter using numbers!");
  23. input.nextLine();
  24. }
  25. } while (continueInput);
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment