ankur trapasiya wrote:can anyone please clear my doubt ?
I will give it a try. ;)
The
reference says that the InputMismatchException is
Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
That's why it is a good idea to use a try-catch. You have a try-catch block in your code, but your while is outside of it.
In the line where you initialize the Scanner you don't define a delimiter so the whitespace is used by default.
In the while-loop you define that the loop will be executed until you don't find "Java" in the tokens that has been build from the Input that you gave to the scanner .
That is the case in the first attempt to find "Java" because the first
word is "Sun" and so the loop stops and the Exception is thrown.
If you would use the word "Sun" as Pattern the loop will be executed once and then stop.
I edited the code a little bit, so that the while is in try-catch block and the Pattern is "Sun" instead of "Java."
You could try Using
tkn = sc.next(".*");
to see what happens. ;)