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
Which one bc im struggling
Setler79 [48]

Answer:

cant really see it

Explanation:

3 0
2 years ago
In other programming languages, the dictionary data structure is referred to as a(an)
hjlf

Answer:

hash

Explanation:

it is called a <u>hash,</u><u> </u><u>a </u><u>map</u><u>,</u><u> </u><u>or </u><u>a </u><u>hasmap</u>

5 0
2 years ago
Read 2 more answers
How can the amount of data in a sampled google analytics report be increased?
goldfiish [28.3K]

Under the reporting interface of google analytics. The reporting tool base the amount of sample on the precision setting of the report. Greater precision will need more sample and less precision setting will need less sample. It is the percent of N session which is changed depending on the setting. You can find more information and official documentation on Google analytics product page.


Greater precision option will provide max sample.

3 0
3 years ago
Television and movies are an important medium of communication. Perform online and offline research, and describe the history of
iren2701 [21]

Answer:

The American film industry, popularly called Hollywood, has played an important role as a platform for communication. It has influenced audiences the world over. The history of Hollywood begins in 1910 with the silent movie In Old California. The 1920s saw the arrival of movies with sound and music. In the 1940s, during and after World War II, movies were made on themes related to war. In the 1950s, Hollywood movies began to compete with television, as television entered American homes. Cutting to the 1990s, the use of animation in movies began to rise. The 2000s saw a continuation of this trend. Movies are now made using the most advanced technology and effects.

Hollywood movies play an important role in the communication process. It can bridge the gap between different communities, cultures, races, and nations. For example, the movie The Help highlighted the gross discrimination the Blacks suffered during the 1960s. Movies have also played a great role in building harmony between cultures. For example, the movie Eat, Pray, Love explores Eastern cultures. Animated movies are enjoyed by movie lovers of all ages, and often end with a good message.

Explanation:

PLATO

7 0
2 years ago
Two small engine technicians are disassembling the engine shown in Figure A-9. After observing the engine components, both techn
11Alexandr11 [23.1K]
<span>A splash-type lubrication system uses what the Technician A says :  that the engine uses a dipper that's attached to the connecting rod to splash oil from the crankcase onto its components. </span>In  the  engine, dippers on the connecting-rod bearing caps enter the oil pan with each revolution to produce the oil splash.  A passage is drilled in each rod from the dipper to the bearing to <span>ensure lubrication.</span> 
3 0
3 years ago
Other questions:
  • The commands available here change depending on the activity taking place in the presentation window's main work area.
    15·2 answers
  • Changing the configuration of a database falls under which category of databaseâ administration?
    15·1 answer
  • BRAINLIEST ANSWER AWARDED PLEASE HELP
    12·1 answer
  • The ________ phase is a technical blueprint for a whole system which captures all aspects of how the system's components will fu
    9·1 answer
  • Which of the following describes the term "false information?"
    5·1 answer
  • When looking at a relationship between two tables on an ERD, the _____ table can be identified by the presence of a foreign key
    8·1 answer
  • Write a program with 2 separate functions which compute the GCD (Greatest Common Denominator) and the LCM (Lowest Common Multipl
    9·1 answer
  • you are packing a mobile phone containing a lithium ion battery and you get a prompt on the screen to put a lithium ion label on
    13·1 answer
  • What is a symbol such as a heavy dot or another character that precedes a text in a power point
    12·1 answer
  • Starting in 2024 under a new law, all new smartphones sold in the eu must have….
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!