Question

Create a simple addition calculator in Java. The program should prompt the user to enter 2...

Create a simple addition calculator in Java. The program should prompt the user to enter 2 integers, then adds the numbers and prints the result. Make sure the program includes appropriate exception handling in case the user does not enter appropriate integer values.

Homework Answers

Answer #1
import java.util.InputMismatchException;
import java.util.Scanner;

public class AddIntegers {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        try {
            System.out.print("Enter first integer: ");
            int n1 = in.nextInt();
            System.out.print("Enter second integer: ");
            int n2 = in.nextInt();
            System.out.println("sum of " + n1 + " and " + n2 + " is " + (n1 + n2));
        } catch (InputMismatchException e) {
            System.out.println("You must enter integers only..");
        }
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
The Java program should sum the odd integers from 1 to the integer that the user...
The Java program should sum the odd integers from 1 to the integer that the user enters. For example, if the user enters 30, the program should sum the odd integers from 1 to 30.   Print "Please enter a positive nonzero integer." Declare a Scanner and obtain an integer from the user.   Use a Do...while statement and calculate the sum Use the integer that the obtained from the previous step for the loop-continuation condition. Display the sum Print the sum...
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail. Example of the expected output is as follows:
4) Create the following in a Java program. Create a Scanner Prompt the user to Receive...
4) Create the following in a Java program. Create a Scanner Prompt the user to Receive the amount and declare the variable double amount for scanner Declare variable integer remaining amount which is equal to (int) (amount*100) Find the number of one dollars bt declaring the integer variable which is equal to remaining amount divided by 100 Declare: remaining amount %=100 Find the number of quarters: Declare integer variable number of quarters equal to remaining amount divided by 25 Declare:...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
x86 irvine library assembly code Write a complete program that: 1. Prompt the user to enter...
x86 irvine library assembly code Write a complete program that: 1. Prompt the user to enter 10 numbers. 2. save those numbers in a 32-bit integer array. 3. Print the array with the same order it was entered. 3. Calculate the sum of the numbers and display it. 4. Calculate the mean of the array and display it. 5. Rotate the members in the array forward one position for 9 times. so the last rotation will display the array in...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a method using JAVA called "maximum4" that returns the maximum of 4 decimal numbers. Use...
Write a method using JAVA called "maximum4" that returns the maximum of 4 decimal numbers. Use the "math.max" method to implement "maximum4". The user should enter the 4 values and the program determines the maximum and then display the result. The steps for the Project could be the following: Create the Maximum4 method that includes the math.max Define and enter the variables Apply the Maximum4 method you created to find the maximum of the 4 decimal numbers you entered and...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...