| 1. |
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. |
|
Answer» ong>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); } } |
|