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
The IT department in a company is looking for a solution to consolidate the functionality of several mission critical server com
marshall27 [118]

Answer: (1) Oracle VM Server  

               (2) VM Ware v Sphere

Explanation:

  The oracle VM server and the VM ware v Sphere are the two hyper-visors that is used in the IT department by an organization for the purpose of high performance.

  • The oracle VM (Virtual machine) server is one of the type of virtual machine that is used to run various types of operating system and oracle VM server is one of the open source technology that support Linux and window.
  • The VM ware V sphere is one of the type of visualization based application which basically run the multiple OS based system by using the single machine. It is also helps in balancing the workload in the system.  

According to the given question, the two given hyper-visors are used by the information technology department that helps in handling the critical functionality in an organization.

 Therefore, The given answer s correct.

8 0
4 years ago
Any anime weebs wanna talk
erastova [34]

Answer:

Sowwy, it's not Me!

The Bermuda Triangle, also known as the Devil's Triangle, is a loosely defined region in the western part of the North Atlantic Ocean where a number of aircraft and ships are said to have disappeared under mysterious circumstances. Most reputable sources dismiss the idea that there is any mystery.[1][2][3]

Bermuda Triangle

Devil's Triangle

6 0
3 years ago
Read 2 more answers
What do you think will happen if you use a different file format for a certain document type?
sweet-ann [11.9K]

Answer:

It will either convert the file automatically into the program so you can edit it on the different platform or it will not be able to open the document at all.

Explanation:

I have personally done this before. Usually I use Word documents, which are .doc and .docx files.

I also run a Linux based OS sometimes (dual boot) and I don't have Microsoft Office products on it. However, Linux does have their own set of products that are similar to it.

I opened a .docx file on the Linux version of Word and it did convert the file into a separate one and I was able to edit it.

However, when I then tried opening the new Linux file type on Word, it was not able to open the document at all.

Therefore, it really depends on what programs have what capabilities. Word can open .doc and .docx. Linux could open their own types as well as .doc and .docx by conversion.

A rule of thumb is just not to change the file format at all. Just stick with the original so you don't lose your file. If you cannot do so, then use a online file converter to change the file type.

3 0
3 years ago
Two types of formulas in Excel, what are they? A. Complex and simple, B. General and currency, C. Logical and Boolean, D. Trig a
lyudmila [28]
A. Complex and simple
4 0
3 years ago
In the current situation, how do you access information as a student? How will you integrate the use of ICT in your daily life a
3241004551 [841]

Answer:

Explanation:

As a computer science student information is accessed in every possible way. This can be through a local school lan network, mobile devices, e-mail, etc. All of these help information flow to and from various people and makes obtaining this information incredibly simple as a student. Especially in the field of computer science, I need to integrate as many ICT devices with me in my everyday life, to send emails, check calendar updates, research information on the web, check school reports, and even speak with clients.

3 0
3 years ago
Other questions:
  • Fill in the missing statements or expressions in the following code that checks to ensure that a user enters an integer value at
    11·1 answer
  • A computer that stores and distributes newsgroup messages is called a newsreader.
    13·1 answer
  • What binary number is the output of adding 1001011 and 100000?
    13·1 answer
  • Henry is designing a document for his architecture project. In which order should Henry preform the different tasks that are req
    7·1 answer
  • Use the Law of Sines to solve the triangle. Round your answers to two decimal places.
    6·1 answer
  • HURRY 50 POINTS A human is needed to create the algorithms that computers use.<br> True<br> False
    14·2 answers
  • The alveolar walls and the pulmonary capillaries form the:
    5·1 answer
  • How do you think Beyoncé choreography has/will influence the future of dance?
    9·2 answers
  • Match each item with a statement below. - Uses two keys: one to encrypt data and one to decrypt data - Encryption algorithm used
    9·1 answer
  • * Describe the five components of a<br> computer
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!