Java Scanner.nextBoolean()
Syntax
Scanner.nextBoolean() has the following syntax.
public boolean nextBoolean()
Example
In the following code shows how to use Scanner.nextBoolean() method.
// ww w . jav a 2s.co m
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String s = "Hello true World! 1 + 1 = 2.0 ";
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
// if the next is boolean, print found and the boolean
if (scanner.hasNextBoolean()) {
System.out.println("Found :" + scanner.nextBoolean());
}
// if a boolean is not found, print "Not Found" and the token
System.out.println("Not Found :" + scanner.next());
}
scanner.close();
}
}
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »