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
Allie needs to add a long row of numbers. She should enter a
ikadub [295]
I believer the answere is a formula hope this helped
8 0
2 years ago
Read 2 more answers
Question 12 (2 points)
Gelneren [198K]
1.) narrative = a THATS all I can answer rn sorryyyy
5 0
3 years ago
Read 2 more answers
Rickie gets a message from the school’s tech support department. Their computer is supposed to be running the latest system soft
raketka [301]

Answer:

the operating system or web browser

6 0
3 years ago
Read 2 more answers
Assume there are 15 students registered for a history class. You will need to declare an array of the StudentType structure to c
andrezito [222]

Answer:

#include <iostream>

using namespace std;

struct StudentType{

   string studentName;

   int studentId;

}

int n;

char answer[20];

int main(){

   cout<< "Enter the size of the array: ";

   cin >> n;

   StudentType *student = new StudentType(n);

   for (int i = 0; i < n; i++){

       int name;

       int number;

       cin>> name;

       cin >> number;

       student[i].studentName = name;

       student[i].studentId = number;

   }

   for (int i = 0; i < 20; i++){

       cout<< "Enter answers: ";

       cin >> ans;

       answer[i] = ans;

   }

}

Explanation:

The C++ source code has three global variables namely, answer which is an array of character data type, StudentType which is a structure data type and the integer variable n. The main function declares and initializes the dynamic-spaced student array of the structure datatype with the n variable.

3 0
3 years ago
How to create a tender statement in database management?
sattari [20]
Persaud them really, and make them fell like you truly know about the topic also, use strong big words and fancy up the fonts
3 0
3 years ago
Other questions:
  • C programming:
    10·1 answer
  • What type of attack intercepts communication between parties to steal or manipulate the data?
    13·1 answer
  • Controlled intersections use some form of which of the following:
    7·1 answer
  • You are the network administrator for a school system. Your boss informs you that she is thinking of implementing a BYOD program
    15·2 answers
  • What is the correct process for selecting an entire row in a spreadsheet?
    8·2 answers
  • 1.What is the term referring to an amount of money that is owed?
    6·2 answers
  • Can someone start me off with a short 2 or 3 paragraphs about the pros and cons of Microsoft Word, and if you can recommend a si
    12·1 answer
  • In terms of CPU scheduling metrics, __________ is the time at which the job completes minus the time at which the job arrived in
    10·1 answer
  • Please help me excel questions!​
    6·2 answers
  • Can you exclude header rows from sort in excel?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!