1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kaylis [27]
3 years ago
14

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.

Computers and Technology
1 answer:
HACTEHA [7]3 years ago
7 0

Answer:

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!");  } }

You might be interested in
How to learning algothrim bett
Stells [14]
The GCSE Computing MOOC (created with Cambridge University Press and Raspberry Pi) can be used either as a course or a flexible teaching resource,
8 0
3 years ago
​Microsoft claims that the microsoft project software can _____.
Lina20 [59]
The Microsoft project software is designed in assisting the project manager in formulating plans, distributing resources to certain tasks, progress monitoring, budget management and analysis of workloads.

I hope I was able to help you. Thank you for posting your question here at Brainly.
8 0
3 years ago
What is incorrect about the following code? Suggest a possible revision of the code to correct the error.
Ad libitum [116K]
This is really cool but no one cares
5 0
2 years ago
Are self driving cars essential for the future of transportation
azamat
Yes, I think so at least because what about those who can't physically drive? They would want the experience of being in the driver's seat.
6 0
3 years ago
you where discussing software piracy with a friend and were surprised to learn how software piracy can impact your life every da
umka2103 [35]
Ahh, software. What is software piracy?
.
Ok... so it sounds evil.
.
BECAUSE IT IS!!
.
Software piracy can get you and suspected accomplices in court.
.
Fines up to a million dollars and lots of jail time.
.
Lots.
.
And lots.
.
But an effect of software piracy is that the software stealer himself earns money...
.
illegaly.
7 0
3 years ago
Other questions:
  • What type of devices are the key board and the mouse?
    10·1 answer
  • Text, numbers,graphics, sounds entered into a computer's memory during input operations are referred to as
    11·1 answer
  • Try writing pseudo code that describes how your device uses input data to perform the action that you want.
    9·1 answer
  • In ancient times what did kings and tribals chiefs use to communicate
    10·1 answer
  • For each 8-bit data frame the layer uses a generator polynomial G(x) = x4+x2+ x+1 to add redundant bits. What is the sequence of
    11·1 answer
  • Given a constant named size with a value of 5, which statement can you use to define and initialize an array of doubles named ga
    12·1 answer
  • Please Help!
    13·1 answer
  • Anyone 13 i a boy 13 pan
    12·2 answers
  • SO I LIKE BOILED EGGS AND MY MUM WAS MAKING SALAD FOR DINNER SO I SAID DID YOU MAKE AND EGG-TRA ONE FOR ME
    13·1 answer
  • Where is the flash drive listed in a windows 7 save as dialog box?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!