Scanner.nextBoolean() has the following syntax.
public boolean nextBoolean()
In the following code shows how to use Scanner.nextBoolean() method.
//from w w w . j a va2 s . 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.