This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ankur trapasiya
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 ?

 
Anupam Jain
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...
 
Goerch Mosi
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. ;)


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic