Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Java program.This is a simple try-catch exercise. The program asks for two numbers and returns the product of the two numbers. The program

Create a Java program.This is a simple try-catch exercise. The program asks for two numbers and returns the product of the two numbers. The program works as it is but if someone enters a word instead of a number, the program crashes. Add a try and catch to the existing code that looks for an InputMismatchException and prints out "A Non-Number value was entered." .

I have this code but it does not run in the compiler I need to use for the assignment.It states it contains an infinite loop:

import java.util.*;

public class InputMismatchCatch {

public static void main(String[] args) { int x=0; Scanner input = new Scanner(System.in);

do{ try{ input = new Scanner(System.in); System.out.println("This program accepts two numbers" + " and returns the product. The first number " + "will be multiplied by the second one Please enter " + "the first number:"); double num1 = input.nextDouble(); System.out.println("Please enter the second number:"); double num2 = input.nextDouble(); System.out.printf("%s multiplied by %s equals %s", num1,num2,(num1*num2)); x=1;

} // end main class catch(InputMismatchException e){ System.out.println(" Non-Number value was entered."); input.next();

}

}while(x==0); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

Explain how to build high-performance service delivery teams.

Answered: 1 week ago

Question

Understand what a service-oriented culture is.

Answered: 1 week ago