Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.*;
- public class ExceptionHandling {
- public static void main(String[] args) throws InputMismatchException {
- Scanner input = new Scanner(System.in);
- boolean continueInput = true;
- // Prompt user to enter two integers
- do {
- try {
- System.out.print("Enter two integers: ");
- int number1 = input.nextInt();
- int number2 = input.nextInt();
- int result = number1 + number2;
- System.out.println(number1 + " + " + number2 + " = " + result);
- continueInput = false;
- }
- catch (InputMismatchException ex) {
- System.out.println("Exception: Invalid entry! Please re-enter using numbers!");
- input.nextLine();
- }
- } while (continueInput);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment