In: Computer Science
Create method addUserInput
Write a method called addUserInput().
The method should ask the user to input two integers (one by one), add the two integers, and return the sum.
By using java.util.Scanner to get user input;
The method may not compile due to Scanner Class which need to add a "throws" statement onto the end of the method header because some lines may throw exceptions
Refer to the Java API documentation on Scanner to figure out which specific Exception should be thrown from your method - do not add “throws Exception”.
I only want the addUserInput() method to write out in code with a specific Exception it needs in the method. In Java, it would be appreciated thanks.
Code is Given Below:
========================
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInputDemo {
//main method
public static void main(String[] args) throws
InputMismatchException {
UserInputDemo ui=new
UserInputDemo();
//calling method to find sum
int sum=ui.addUserInput();
System.out.println("Sum is :
"+sum);
}
//creating method
public int addUserInput() throws
InputMismatchException{
//creating scanner object to get
user input
Scanner scn=new
Scanner(System.in);
//asking user for input
System.out.print("Enter first
Number : ");
int num1=scn.nextInt();
System.out.print("Enter second
Number : ");
int num2=scn.nextInt();
//finding sum and returning
it
return num1+num2;
}
}
Output:
===============
Code is Given Below:
===================