• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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. ;)


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!