Question: 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
The execution of the code will be different in both the cases as In first case even if we ... View full answer
Get step-by-step solutions from verified subject matter experts
