Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. ;)


 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic