mobslayer88
15.10.2020 •
Computers and Technology
Create a brief program that demonstrates use of a Java exception. For example, you could use InputMismatchException and ask the user to input an integer and show that the program executes correctly if they enter an integer and also show that the exception is thrown and a proper error message is displayed to the user if they input a string of letters. You may want to try it with and without a try catch block for practice, but either one will be sufficient for credit.
Solved
Show answers
More tips
- A Art and Culture How to Learn to Play Guitar?...
- S Style and Beauty What to Do When Hair Starts Falling Out?...
- H Health and Medicine Novomin: What is it and how to use it?...
- P Philosophy Unbelievable stories of encounters with otherworldly forces...
- L Leisure and Entertainment How to Choose the Perfect Gift for Men on February 23rd?...
- H Health and Medicine How to Treat Whooping Cough in Children?...
- H Health and Medicine Simple Ways to Lower Cholesterol in the Blood: Tips and Tricks...
- O Other How to Choose the Best Answer to Your Question on The Grand Question ?...
- L Leisure and Entertainment History of International Women s Day: When Did the Celebration of March 8th Begin?...
- S Style and Beauty Intimate Haircut: The Reasons, Popularity, and Risks...
Answers on questions: Computers and Technology
- C Computers and Technology How Do You Get Rid Of This?...
- M Mathematics 5 Mei wrote the subtraction expression shown. She will rewrite it as an addition expression to find its value. - 8- (-4) - 9 Part A Write a number in each blank to show an equivalent...
- S Social Studies Question: The majority of Canadians live near the United States borders because northern Canada is mostly . A. tundra B. arid desert C. tropical rainforest D. humid continental...
Ответ:
Here is the program that demonstrates the use of JAVA exception:
import java.util.Scanner; //to accept input from user
public class Main { //class name
public static void main(String[] args) { //start of main method
Scanner input = new java.util.Scanner(System.in); //creates Scanner class object to accept input from user
int number1 = 0; //stores the first integer value
int number2 = 0; //stores the second integer value
while(true) { //keeps looping until user enters integer value
System.out.println("Enter 2 integers to perform addition: "); //prompts user to enter two integer values
try { //defines a chunk of code to check for errors
number1 = input.nextInt(); //reads input integer 1
number2 = input.nextInt(); //reads input integer 2
break; }
catch (java.util.InputMismatchException e) { // defines a code chunk to execute if an error occurs in the try code chunk
System.out.println("Input must be an integer "); //displays this message if user enters anything other than an integer value
input.nextLine(); } } // reads input from user again until user enters both integer type values
System.out.println("The sum is: " + (number1+number2)); } } //if user enters 2 integers then computes and displays the sum of two integer values
Explanation:
The program uses InputMismatchException exception and asks the user to input two integers and computes the sum of two integers if user enters integers otherwise an exception InputMismatchException is thrown and a error message Input must be an integer is displayed to the user if they input a string of letters instead o f integer values. Here while loop is used which keeps executing until user enters both the integer values. After the user enters correct values, the sum of the two integers are computed and result is displayed on output screen. The screenshot of program and its output is attached.
Another program asks the user to input an integer and the program executes correctly if they enter an integer but exception InputMismatchException is thrown with an error message that is is displayed to the user if they input a string of letters. Here is the program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new java.util.Scanner(System.in);
int number1 = 0;
while(true) {
System.out.println("Enter an integer value: ");
try {
number1 = input.nextInt();
break; }
catch (java.util.InputMismatchException e) {
System.out.println("Input must be an integer ");
input.nextLine(); } }
System.out.println("The program executed correctly!"); } }
Ответ:
what
Explanation: