Consider the code that follows. What does it do?

Consider the code that follows. What does it do?

String value = "2";
boolean tryAgain = true;
while (tryAgain == true)
{
try
{
int num = Integer.parseInt(value);
tryAgain = false;
}
System.out.println("Valid integer");
catch(NumberFormatException nfe)
{
System.out.println("Invalid integer");
System.out.print("Enter an integer");
value = sc.next
}
}

a. It prints "Invalid integer" to the console.
b. The code compiles but causes a runtime error.
c. The code doesn't compile.
d. It prints "Valid integer" to the console.


Answer: The code doesn't compile.

Output example:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:818)
at java.util.Scanner.next(Scanner.java:1420)
at java.util.Scanner.nextDouble(Scanner.java:2324)
at FutureValueApp.main(FutureValueApp.java:17)


Learn More :