Java Scanner nextBoolean() MethodThe nextBoolean() is a method of Java Scanner class which is used to scan the next token of the input into a boolean value and returns that value. If the translation is successful, the scanner past the input that matched. SyntaxFollowing is the declaration of nextBoolean() method: ParameterThis method does not accept any parameter. ReturnsThe nextBoolean() method returns the boolean scanned from the input. ExceptionsInputMismatchException- It will thrown this Exception if the next token is not a valid boolean. NoSuchElementException- It will thrown this Exception if the input is exhausted. IllegalStateException- It will thrown this Exception if the innvocation is done after Scanner is closed. Compatibility VersionJava 1.5 and above Example 1Output: Are you above 18?- True You are over 18 Are you above 18?- False You are under 18 Example 2Output: Not Found Boolean:Hello Found Boolean:true Not Found Boolean:World! Not Found Boolean:12 Found Boolean:false Not Found Boolean:15 Not Found Boolean:2.123 Example 3Output: Would you like to play? : True Great! This will be fun. Would you like to play? : False Maybe next time Next TopicJava-scanner-nextbyte-method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India