Question

In Java - Write a program that prompts the user to enter two integers and displays...

In Java -

Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.*;
class Assignment
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int first,second;
while(true)
{
try
{
System.out.println("Enter first integer");
first = sc.nextInt();
break;
}
catch (InputMismatchException e)
{
System.out.println("INPUT AN INTEGER ONLY,TRY AGAIN.");
sc.nextLine();
}
}
while(true)
{
try
{
System.out.println("Enter second integer");
second = sc.nextInt();
break;
}
catch (InputMismatchException e)
{
System.out.println("INPUT AN INTEGER ONLY,TRY AGAIN.");
sc.nextLine();
}
}
System.out.println("The sum of "+first+" and "+second+" is "+(first+second));
}
}

Output:

Options Enter first integer 10 Enter second integer INPUT AN INTEGER ONLY, TRY AGAIN Enter second integer 20 The sum of 10 an

Add a comment
Know the answer?
Add Answer to:
In Java - Write a program that prompts the user to enter two integers and displays...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • Write a program that prompts the user to read three positive hex integers and display their...

    Write a program that prompts the user to read three positive hex integers and display their sum in both hex and decimal. Your program should prompt the user to read the number again, if the input is incorrect. You must use java.lang.NumberFormatException. (chapter12.AddThreeHexNumbers.java)

  • Java: Write a program that prompts the user to enter integers in the range 1 to...

    Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...

  • In C++, Write a program that it prompts to user for two integers, computes the quotient...

    In C++, Write a program that it prompts to user for two integers, computes the quotient of two integers, and then displays the result. You need to do the following things in this assignment: + The main function prompts for user input and reads the two integers that the user entered. + Write a quotient function that computes the quotient of two integers and return the results. (hint: avoid integer division and divide by zero)

  • Write java program that prompts the user to enter integers from the keyboard one at a...

    Write java program that prompts the user to enter integers from the keyboard one at a time. Program stops reading integers once the user enters the same value three times consecutively. Program then outputs "Same entered 3 in a row. "

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • (Process a string) Write a program that prompts the user to enter a string and displays...

    (Process a string) Write a program that prompts the user to enter a string and displays its length and its first character. java StringLength Enter a string:Does quantum determinacy have anything to with whether human cognition is deterministic? The string is of length 88 and the first character is D import java.util.Scanner; class StringLength{    public static void main (String args[]){        Scanner input = new Scanner(System.in);        System.out.println("Enter a string:");        String ans = input.nextLine();        System.out.println("The string...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT