when i run the following code in java:
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
try{
long x=sc.nextLong();
System.out.println(x);
}
catch(Exception e){
System.out.println(sc.next()+" can't be fitted anywhere.");
}
sc.close();
}
}
and enter the input as "23333333333333333333333333333333333333333" it gives the following output :
23333333333333333333333333333333333333333 can't be fitted anywhere.
When sc.nextLong()
throws InputMismatchException
then how does the sc.next()
in the catch block get the exact same value entered for sc.nextLong()
in try block? Should it not ask for the input from consoleenter code here?