Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The following two codes are executed for which a wrong input of 4.3 is entered. How the execution of these codes will be different
The following two codes are executed for which a wrong input of 4.3 is entered. How the execution of these codes will be different and why? [2 points] import java.util.Scanner; import java.util.InputMismatchException; import java.util.InputMismatchException; import java.util.Scanner; public class Q1 { public static void main(String [] args) { public class Q3 { public static void main(String [] args) { int num = getNumber(); System.out.println("The entered number is: " + num); int num = getNumber(); System.out.println("The entered number is: " +num); public static int getNumber() { public static int getNumber() { Scanner sc = new Scanner(System. in); int input = e; boolean finished = false; while (Ifinished) { Scanner sc = new Scanner (System. in); int input = 0; boolean finished = false; while (Ifinished) { try { System.out.print("Enter a whole number:"); input = sc.nextInt(); finished = true; } catch (InputMismatchException e) { sc.nextline(); System.out.println("Wrong Input, try again..."); System.exit(e); try { System.out.print ("Enter a whole number:"); input = sc.nextInt(); finished = true; catch (InputMismatchException e) { sc.nextline(); System.out.println("Wrong Input, try again..."); } return input; } return input;
Step by Step Solution
★★★★★
3.48 Rating (164 Votes )
There are 3 Steps involved in it
Step: 1
The execution of the code will be different in both the cases as In first case even if we ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started