Scanner nextBoolean() method in Java with Examples
Last Updated :
12 Oct, 2018
The nextBoolean() method of java.util.Scanner class scans the next token of the input as a Boolean. If the translation is successful, the scanner advances past the input that matched.
Syntax:
public boolean nextBoolean()
Parameters: The function does not accepts any parameter.
Return Value: This function returns the Boolean scanned from the input.
Exceptions: The function throws three exceptions as described below:
- InputMismatchException: if the next token is not a valid boolean
- NoSuchElementException: throws if input is exhausted
- IllegalStateException: throws if this scanner is closed
Below programs illustrate the above function:
Program 1:
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Gfg true 9 + 6 = 12.0 false" ;
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
if (scanner.hasNextBoolean()) {
System.out.println( "Found Boolean value :"
+ scanner.nextBoolean());
}
else {
System.out.println( "Not found Boolean() value :"
+ scanner.next());
}
}
scanner.close();
}
}
|
Output:
Not found Boolean() value :Gfg
Found Boolean value :true
Not found Boolean() value :9
Not found Boolean() value :+
Not found Boolean() value :6
Not found Boolean() value :=
Not found Boolean() value :12.0
Found Boolean value :false
Program 2: To demonstrate InputMismatchException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Gfg 9 + 6 = 12.0" ;
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
System.out.println( "Next Boolean value :"
+ scanner.nextBoolean());
}
scanner.close();
}
catch (Exception e) {
System.out.println( "Exception thrown: " + e);
}
}
}
|
Output:
Exception thrown: java.util.InputMismatchException
Program 3: To demonstrate NoSuchElementException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Gfg" ;
Scanner scanner = new Scanner(s);
for ( int i = 0 ; i < 5 ; i++) {
if (scanner.hasNextBoolean()) {
System.out.println( "Found Boolean value :"
+ scanner.nextBoolean());
}
else {
System.out.println( "Not found Boolean value :"
+ scanner.next());
}
}
scanner.close();
}
catch (Exception e) {
System.out.println( "Exception thrown: " + e);
}
}
}
|
Output:
Not found Boolean value :Gfg
Exception thrown: java.util.NoSuchElementException
Program 4: To demonstrate IllegalStateException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
String s = "Gfg 9 + 6 = 12.0" ;
Scanner scanner = new Scanner(s);
scanner.close();
System.out.println( "Scanner Closed" );
System.out.println( "Trying to get "
+ "next Boolean value" );
while (scanner.hasNext()) {
if (scanner.hasNextBoolean()) {
System.out.println( "Found Boolean value :"
+ scanner.nextBoolean());
}
else {
System.out.println( "Not found Boolean value :"
+ scanner.next());
}
}
}
catch (Exception e) {
System.out.println( "Exception thrown: " + e);
}
}
}
|
Output:
Scanner Closed
Trying to get next Boolean value
Exception thrown: java.lang.IllegalStateException: Scanner closed
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextBoolean()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...