Question

In: Computer Science

For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner if the input does not match the type requested.

Solutions

Expert Solution

Please upvote if you like the answer, as it helps the community a lot.

Also if you have any doubts, feel free to ask in comments, we will reach you ASAP.

Code(Solution.Java):

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be same as file name. */
class Solution
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       Scanner sc = new Scanner(System.in);

//Try Catch Block
       try{
           int x = sc.nextInt();
           System.out.println("You entered "+x);
       } catch(InputMismatchException e) {
           System.out.println("This is an invalid entry, please enter an integer");
       }
   }
}

SAMPLE I/Os:

Input:

42

Output:

You entered 42

Input:

Alex

Output:

This is an invalid entry, please enter an integer


Related Solutions

Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
1) Describe briefly what exceptions are and what their purpose is. What is the try block?...
1) Describe briefly what exceptions are and what their purpose is. What is the try block? What is the catch block? What happens with the flow of control if the catch block does not catch a thrown exception? Give a simple example. 2) Describe briefly what templates are and what their purpose is. 3) Write one line of code for each line below. What is the template type in each case? - Create a vector to store student IDs -...
Instead of using int.TryParse, use int.Parse instead and use try/catch in C# to handle possible errors...
Instead of using int.TryParse, use int.Parse instead and use try/catch in C# to handle possible errors using System; using System.Collections.Generic;                    public class Program {    public static void Main()    {        List<int> list = new List<int>();               Console.WriteLine("Please input integers: (in one line)");                      string input = Console.ReadLine();        string[] tokens = input.Split(); // split string into tokens "12 13 10" -> "12" "13"...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are it's benefits? What does it mean when we raise a custom exception?
Find the readData() method Inside this method is a try block without any catch statements. Add...
Find the readData() method Inside this method is a try block without any catch statements. Add the following 5 catch statements to this file (Hint: put them in the right order): RuntimeException Exception FileNotFoundException NumberFormatException InputMismatchException For each of these print out a short message describing the exception that occurred Java Class import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; public class ReadInData {    public static double readData(String fileName) {        File file = new File(fileName);       ...
IN PYTHON Your task this week is to use loops to both validate user input as...
IN PYTHON Your task this week is to use loops to both validate user input as well as automate a test suite for your lab solution from last week. Your program will prompt the user for the cost of their groceries. Then you are to calculate the value of the coupon to be awarded based on the amount of the grocery bill. Use the table below to determine coupon amounts. Money Spent   Coupon Percentage Less than $10   0% From $10...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
PLease using C# Without using try-catch-throw for exception handling (Ch07), display an error message (using csc...
PLease using C# Without using try-catch-throw for exception handling (Ch07), display an error message (using csc or VS2017) when an input is invalid (i.e., no data, wrong format, wrong type, etc.) and fails the type conversion. Generate random integers from any of the following three non-overlapped ranges -14 ~ -7 -2 ~ 5 33 ~ 44 Numbers generated must also randomly distributed across the three ranges.
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT