Pages

Friday, 18 November 2016

Chapter 12 Exercise 2, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

12.2 Write 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.


import java.util.InputMismatchException;
import java.util.Scanner;

public class Exercise_02 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter two integers: ");
        int num1 = 0;
        int num2 = 0;
        boolean isValid = false;
        while (!isValid) {
            try {
                num1 = input.nextInt();
                num2 = input.nextInt();
                isValid = true;
            } catch (InputMismatchException ex) {

                input.nextLine();
                System.out.println("Invalid input..." );
                System.out.print("Enter two integers: ");
            }
        }
        System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
    }
}

No comments :

Post a Comment