java.util.Scanner nextBoolean()

Description :

This java tutorial shows how to use the nextBoolean method of Scanner class of java.util package. This method returns boolean data type which corresponds to the interpreted boolean value of the scanner input.

Method Syntax :

public boolean nextBoolean()

Parameter Input :

 

DataType Parameter Description
N/A N/A N/A

 

Method Returns :

This method simply returns the next token of the input boolean value.

Compatibility Version :

Requires Java 1.5 and up

Exception :

InputMismatchException

– will be thrown by this Scanner class if the token is not a valid boolean. A valid boolean is either “true” or “false” regardless of case.

NoSuchElementException

– The NoSuchElementException will be thrown by the nextBoolea method is invoked after all the scanner tokens are already exhausted.

IllegalStateException

– will be thrown by the nextBoolean method if the method is invoked after the scanner is already closed.

Discussion :

The Scanner nextBoolean() method is usually used to as a flag in java code logic. Like for instance, we are reading a file and there is a specific token to look at in order for our code to proceed further in reading the file and the scanner object nextboolean method is a very convenient way to handle the boolean token. Well of course there is an old way to handle this kind of scenario like doing the if and else condition, however using this method is much easier for implementation purposes.

Java Code Example :

This java example source code demonstrates the use of nextBoolean method of Scanner class. Basically the example used a boolean token to validate the printing of contents or halt the program.

package com.teknoscope.java.tutorial.scanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/*
 * Example java source code on the usage of
 * Scanner NextBoolean() method
 * Print the contents of a file using Scanner
 */

public class ScannerNextBoolean {

	public static void main(String[] args) throws FileNotFoundException {
		// Declare File object
		File file = new File("E:/tmp/java/tutorial/scanner/example.txt");
		// initialize the scanner
		Scanner scan = new Scanner(file);
		// iterate through the file line by line
		while(scan.hasNextLine()){
			// check if token consists of boolean
			if(scan.hasNextBoolean()){
				// if the token is true, print the token otherwise exit the loop
				if(scan.nextBoolean()){
					System.out.println(scan.nextLine());
				}
				else{
					break;
				}
			}
			// printing the token
			else{
				System.out.println(scan.nextLine());
			}
		}
		scan.close();

	}
}

File Contents:

true
scanner boolean check
false
I want to omit this
************

Sample Output :

Running the nextBoolean() method example source code of Scanner class will give you the following output

java scanner nextboolean method example

java scanner nextboolean method example

Exception Scenario :

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextBoolean(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextBoolean.main(ScannerNextBoolean.java:29)

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextBoolean(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextBoolean.main(ScannerNextBoolean.java:39)

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextBoolean(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextBoolean.main(ScannerNextBoolean.java:38)

Similar Method :

  • N/A

Suggested Reading List :

References :