Write a program that prompts the user to read two integers and displays their su
ID: 3543669 • Letter: W
Question
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.
I AM HAVING TROUBLE WITH PROMPTING THE USER TO,READ THE NUMBER AGAIN IF THE INPUT IS INCORRECT!
Here is the code that I have
import java.io.*;
public static void main(String[]
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
int num1 = 0, num2 = 0;
String str1, str2;
try
{
System. out.print("Enter first number ");
str1 = console.readLine();
num1 = Integer.parseInt(str1);
System.out.print("Enter second number ");
str2 = console.readLine();
num2 = Integer.parseInt(str2);
}
catch(IOException e)
{
System.out.println("Input error");
System.exit(1);
}
catch(NumberFormatException e)
{
System.out.println(e.getMessage() + " is not integer");
System.exit(1);
}
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
}
Explanation / Answer
you got answer ??
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.