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 is a benefit of the intranet?
olasank [31]
Hello there.

What is a benefit of the internet?

Access and share data info fast and safe.
8 0
3 years ago
Read 2 more answers
use the following binary search tree to answer the questions. a. how many comparisons are needed to search for ""2"" in this tre
Nesterboy [21]

A data structure called a binary search tree makes it simple to keep track of a sorted list of numbers.

<h3>What is a binary search tree?</h3>

A binary search tree, also known as an ordered binary tree or a sorted binary tree in computer science, is a rooted binary tree data structure where each internal node's key is higher than all the keys in its left subtree and less than all the keys in its right subtree.

A data structure called a binary search tree makes it simple to keep track of a sorted list of numbers. Because each tree node has a maximum of two offspring, it is known as a binary tree. It can be used to search for the presence of a number in O(log(n)) time.

A binary search tree (BST) is a specific type of binary tree in which every node has a comparable key and a connected or associated value.

To learn more about binary search trees refer to:

brainly.com/question/28295292

#SPJ4

6 0
1 year ago
Which of the following could occur when both strong and weak ciphers are configured on a VPN concentrator? (Select TWO) A. An at
Elza [17]

Answer:

A. An attacker could potentially perform a downgrade attack.

E. The IPSec payload reverted to 16-bit sequence numbers.

Explanation:

When both strong and weak ciphers are configured on a VPN concentrator, the following are likely to occur;

An attacker could potentially perform a downgrade attack

The IPSec payload reverted to 16-bit sequence numbers

3 0
3 years ago
Give short answers.
Mkey [24]

Answer:

H) Well it depends how you put it. I'm Doing virtual School for my education. And it is so easy when we have technology out their to provide the help that we need. So I think it has affected education pretty good. You can Also google the answers.

I) Looking up Answers at home

J) Parents can check up on their child's school work and see if they can help them. Also The law is involved in Education.

K) It depend's on how you put it

Explanation:

Short Answers

6 0
2 years ago
Omar sends a PDF file to his teacher. He then prepares another copy of the presentation that includes only the slides he wants t
maria [59]

Answer:

D. Custom Slide Show

Explanation:

Correct on edg

7 0
3 years ago
Other questions:
  • Why are advanced features helpful when businesses use spreadsheets
    5·1 answer
  • What is required for sitting with your seatbelt on while driving?
    7·1 answer
  • True or false there is no relationship between the purpose of the page and the page quality
    13·1 answer
  • Indicate whether the statement is true or false. ____ 1. Autoglobal array elements are referred to with an index number. ____ 2.
    9·1 answer
  • Some commands listed in a menu cannot be selected.<br><br><br> True or False
    13·2 answers
  • A deluxe meal, represented by a DeluxeMeal object, includes a side dish and a drink for an additional cost of $3. The DeluxeMeal
    14·1 answer
  • Pleasee helpppppppppppppppppppppp me!
    7·1 answer
  • The science of networking is attributed to which government's involvement?
    11·1 answer
  • Explain why you would use the soft on/off jumper when working on ATX system
    14·1 answer
  • What is the significance of the scientific method?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!