You're welcome.
Dushyant Shukla wrote:The code is working fine after the changes besides the line 72 (pa=a.nextBoolean()).whenever i enter a yes/no or 1/0 it says Exception in thread "main" java.util.InputMismatchException....
what can be the possible errors...
Have you tried entering true/false?
When you're using a
, if you want to be able to cope with unexpected input you sometimes need to use the "
has..." methods. They tell you if the following input can be parsed as a particular type. So if you're about to use
nextBoolean(), you can check
hasNextBoolean() first. If that returns false, it means you're going to get an exception when you call
nextBoolean().
And if you look at the Javadocs for
java.util.Scanner#hasNextBoolean(), you'll see it says:
Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".
If you want to be able to enter yes/no, you need to read the input as a
String and compare it against String values.