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
What Intel socket recommends the use of a liquid cooling system?
Marizza181 [45]
The Intel socket that Intel recommends you use a liquid-cooling system on is the LGA2011.
5 0
3 years ago
Q10: You cannot rename a compressed folder.
user100 [1]
<h2>Answer:</h2><h2>Oh yes you can. </h2>

Click slowly twice the folder.

Right click and choose rename.

4 0
3 years ago
Explain how abstraction is used in a GPS system?
zhenek [66]
I hope that this answer will help u to solve your problem. But u have tto take the importance clues only .

5 0
3 years ago
When my phone powers on does it take away 1 percent of battery?
ryzh [129]

Answer:

No, battery is based on your phone activity. Simply turning your phone on will not take away battery percentage.

7 0
3 years ago
Read 2 more answers
What errors does the grammar tool find? Choose all that apply
Anestetic [448]

Answer:

2

Explanation:

Because comma splices are not a thing.

(at least i don't think they are)

7 0
3 years ago
Other questions:
  • If you enter 234.567 into a cell that is formatted to display 1 decimal point place, what is the value stored in the cell
    9·1 answer
  • What menu allows you to look up a command and learn how to use it?
    14·1 answer
  • What are the first two lines of defense a company should take when addressing security risks?
    10·1 answer
  • Which of the following is the main consideration when choosing an appropriate outlet box?
    7·2 answers
  • Science is a body of knowledge that extends back to Select one: a. the time of Galileo. b. Italy in the 16th century. c. Greece
    6·1 answer
  • How do I sign into raterhub on my phone and my mobile device at the same time to complete phase 2 of my raters exam for sykeshom
    10·1 answer
  • Is anyone really good at immerse 2 learn??
    9·1 answer
  • The Linux operating system was created by which software engineer?
    15·1 answer
  • How many report charts can be added to the account page layout to meet this requirement? A sales manager would like to look at a
    14·1 answer
  • program 2. write a VB.NET program to solve the linear equation of the form Ax+B=C, i.e x=(C=B)/A (Eg:2x+3=7, where B and C are c
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!