In this article I wrote a program in Java to ask the user to give three numbers and the program will compute the sum of the three number with exception handling capabilities to check for invalid entries.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.
Sample Program Output
Program Listing
Addition.java
package exceptiondemo;
import java.util.Scanner;
import java.util.InputMismatchException;
/**
* NetBeans IDE 8.2
* @author Mr. Jake R. Pomperada
* March 20, 2018 Tuesday
* Bacolod City, Negros Occidental
*/
public class Addition {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum =0;
boolean goodData = false;
while(!goodData) {
try {
System.out.print("ADDITION OF THREE NUMBERS USING EXCEPTION");
System.out.println("\n");
System.out.print("Enter First Value : ");
int num1=sc.nextInt();
System.out.print("Enter Second Value : ");
int num2=sc.nextInt();
System.out.print("Enter Third Value : ");
int num3=sc.nextInt();
sum = (num1+num2+num3);
System.out.println("\n");
System.out.println("The sum of "+ num1 + ", " +num2 + " and "
+ num3 + " is " + sum +".");
System.out.println("\n");
goodData = true;
} catch(InputMismatchException e) {
sc.next();
System.out.println("You Entered a Bad Data." );
System.out.println("Please Try Again." );
System.out.println("\n");
}
} // while loop end
System.out.print("\t END OF PROGRAM");
System.out.println("\n");
}
}
No comments:
Post a Comment