Computer Science, asked by patel9898yp, 1 month ago

Inputmismatchexception writes a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

Answers

Answered by imtiyazallam
0

Answer:

import java.util.Scanner;

import java.util.InputMismatchException;

public class Test {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       boolean isInvalid;

       int a = 0, b = 0, c;

       do {

           c = 0;

           System.out.print("Enter 1st number: ");

           try {

               a = sc.nextInt();

           } catch (InputMismatchException e) {

               sc.next();

               System.out.println("You entered an invalid value");

               c = 1;

           }

       } while (c != 0);

       do {

           c = 0;

               System.out.print("Enter 2nd number: ");

           try {

               a = sc.nextInt();

           } catch (InputMismatchException e) {

               System.out.println("You entered an invalid value");

               c = 1;

               sc.next();

           }

       } while (c != 0);

   }

}

Attachments:
Similar questions