Question

In: Computer Science

Exercise 1: You are required to re-write the following code to catch the exception that can...

Exercise 1:

You are required to re-write the following code to catch the exception that can occur.

import java.util.Scanner;

public class ExceptionDemo

{

public static void main(String[] args)

{

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");

int number = scanner.nextInt();

// Display the result

System.out.println( "The number entered is " + number);

          }

}

Solutions

Expert Solution

import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.print("Enter an integer: "); //try block to find exception
            int number = scanner.nextInt();
            System.out.println("The number entered is " + number);
        } catch (InputMismatchException e) { //catch block to catch exception
            e.printStackTrace();
        }
    }
}

Output:-

Note:- Please comment if you face any problem. :)


Related Solutions

Exercise 2: Try-Catch Exercise Write a Java code that does the following: Create a class MyClass...
Exercise 2: Try-Catch Exercise Write a Java code that does the following: Create a class MyClass and create three methods myMethod1(), Method2() and Method3(). Invoke Method2() from Method1() and Method3() from Method2(). Write a code that can throw an exception inside myMethod3() and compile: File file=new File("filename.txt"); Scanner sc=new Scanner(file); You will get compilation errors now, as you are not handling a checked exception FileNotFoundException. Declare throws over myMethod3() and myMethod2(). You will need to add throws FileNotFoundException on myMethod()...
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This...
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Raising an Exception In the previous problem, you used a try/except statement to catch an exception....
Raising an Exception In the previous problem, you used a try/except statement to catch an exception. This problem deals with the opposite situation: raising an exception in the first place. One common situation in which you will want to raise an exception is where you need to indicate that some precondition that your code relies upon has not been met. (You may also see the assert statement used for this purpose, but we won’t cover that here.) Write a function...
catch me if you can (2002) write a report about catch me if you can (2002)...
catch me if you can (2002) write a report about catch me if you can (2002) must be 900 words that fully describes the movie including describing the main characters, the fraud situation in the movie, and how the situation plays out. The second part of the paper should discuss the actual persons and/or companies on which the movie focuses: Identify these actual persons / company; identify the type(s) of fraud(s) are alleged in the film; discuss your personal feelings...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 # Ask user for a number using an input statement (no prompt) & assign the input to a variable # Convert the user entry to an integer and assign it to a variable # Condition: user entry is greater than zero # Action: display the following word on screen: positive # Exercise 2 # Ask user for a number using an input statement (no prompt)...
I do not know how to: D. Apply exception handling using try-catch for System.OutOfMemoryException. the code...
I do not know how to: D. Apply exception handling using try-catch for System.OutOfMemoryException. the code i have in POWERSHELL: while($true) { $input= Read-Host "Enter the option 1-5" if ($input -eq 5) { break } #B. Create a “switch” statement that continues to prompt a user by doing each of the following activities, until a user presses key 5: switch ( $input ){ #Using a regular expression, list files within the Requirements1 folder, with the .log file extension and redirect...
Question 1 In this exercise, you are required to do a program analysis and write a...
Question 1 In this exercise, you are required to do a program analysis and write a program for a system that computes a speeding ticket cost. The charge is calculated as follows: P20 for every kilometre above the speed limit, plus P100 standard charge. E.g. If the speed limit is 60 km/hour and you are driving at 75 km/hour. The charge will be P400. If the speed is equal to or under the speed limit, then there is no charge....
a) i) The following VHDL code contains erroneous syntax. Re write the code in its corrected...
a) i) The following VHDL code contains erroneous syntax. Re write the code in its corrected format onto your answer sheet. You may assume that din is a 16-bit vector and that the ld, lr and cl inputs are 1-bit wide. lp: process(clk) signal reg : std_logic_vector(15 downt begin if cl=’1’ then reg := (others:=’0’); else if clk=’1’ and clkevent then if ld=’1’ reg <= din; end if; if lr=’1’ then reg := reg(14 downto 0) & “0 else reg...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask the user to input a name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the user input on screen #Exercise 2 #Ask the user to input his/her first name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT