wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem in understanding the output of the question in ExamLab mock test.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem in understanding the output of the question in ExamLab mock test.." Watch "problem in understanding the output of the question in ExamLab mock test.." New topic
Author

problem in understanding the output of the question in ExamLab mock test..

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

This question is from ExamLab Practice exam 1


output: Runtime Exception : InputMisMatchException

The problem here is that i am using Scanner.next(String Pattern) method to find out the occurrences and in answer findInLine method is used. I read the J2SE documentation of both the methods which is the exactly same....then why my answer gives me InputMisMatchException ??? It should work with next also ....

can anyone please clear my doubt ?


OCPJP(83%)
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

ankur trapasiya wrote:This question is from ExamLab Practice exam 1


output: Runtime Exception : InputMisMatchException

The problem here is that i am using Scanner.next(String Pattern) method to find out the occurrences and in answer findInLine method is used. I read the J2SE documentation of both the methods which is the exactly same....then why my answer gives me InputMisMatchException ??? It should work with next also ....

can anyone please clear my doubt ?




Hmmmm...
Still looking for the answer... couldn't get it though...
Would be interesting to know the truth behind...

Anyone else looking for the answer??

I guess the answer lies in the way these two methods work...
As the documentation says here...
JavaDocs

next() methods do make use of the delimiter patterns while the findInLine() method doesn't.
That's where the solution lies... I guess... but I'm not sure...


SCJP-6.0 OCPJWCD-5.0
Goerch Mosi
Greenhorn

Joined: Dec 27, 2010
Posts: 8
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. ;)



OCPJP - 80%
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem in understanding the output of the question in ExamLab mock test..
 
Similar Threads
Looping in exceptions
using Scanner class in search
Searching using Scanner class exp. in K&B SCJP6
How to validate a Scanner keyboard input?
Scanner class