submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args)...

90.2K

Verified Solution

Question

Programming

submit the following files:

  • Main.java

Consider the following code:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = newScanner(System.in);

String input = \"\";

System.out.println(\"Enter the firstnumber: \");

input = in.nextLine();

int a = 0;

int b = 0;

a = Integer.parseInt(input);

System.out.println(\"Enter the secondnumber: \");

input = in.nextLine();

b = Integer.parseInt(input);

int result = 0;

result = a/b;

System.out.println(\"a/b : \" +result);

}

Copy the code to your Main.java file and run it with thefollowing inputs:

  1. 34 and 0;

  2. ‘Thirty-four’ as the first input.

What prevented the program from running successfully?

Update the code, so that it can handle cases a) and b).

Please note the following:

  • You have to use try-catch to handle exceptions. You willget zero credit otherwise;

  • To determine exception classes, that you need to catch, useerror message from the respective case (a) and b)). Hint: to handleString-to-integer conversion, use NumberFormatException;

  • Exceptions should be caught so that:

    • Code never crashes due to non-numeric input (both for first andsecond values);

    • Code never crashes due to division by zero;

    • If non-numeric value is entered for a or b, program shouldoutput \"Error: entered value is not an integer number. Exiting...\"and exit (use System.exit(0); to exit);

    • If zero value is entered for b, program should output \" Error:cannot divide by zero. Exiting...\" and exit (use System.exit(0); toexit);

  • Hints: Use 3 try-catch blocks in total. Try block should onlycontain the command, that can throw an exception.

PLEASE FOLLOW ALL THE INSTRUCTIONSPRECISELY

Answer & Explanation Solved by verified expert
4.5 Ratings (688 Votes)
Updated codeimport javautilScannerpublic class Main public static void mainString args Scanner in new ScannerSysteminString input    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students