There are multiple ways to convert a String to Boolean object type.On this document we would be showing two solution which is using parseBoolean and valueOf. Both of these methods accepts String argument and returns Boolean data type.  The method argument accepts only two possible values in String format which is “true” or “false”.

To understand the presented example, lets tackle first what is a Boolean object type. How does it differ with primitive boolean? The Boolean is a wrapper class of boolean primitive data type. Well, do not worry too much on the primitive and Object type since as of Java 5, autoboxing and unboxing are already in place. The discussion on the autoboxing and unboxing concept is beyond the scope of this document.

As we have said earlier, there are multiple ways to convert a String object to Boolean. On this document we would tackle two ways, one is to use parseBoolean and the second one is to use valueOf.

Instruction in String to Boolean conversion in Java using parseBoolean

The following are the instructions on below examples.

  • Get the user input from Console using Scanner
  • Convert the string input to Boolean object
  • Use the boolean object for code logic wherein if the input is true the console will output YES, otherwise NO.

Java API Used on this example

The java.util.Scanner can parse the tokens into primitive data types using java regular expressions. One of the constructor of the Scanner class can take InputStream which would be leveraging to get the user input from console.

  • Scanner nextLine() – This method returns a String which corresponds to the skipped line of the Scanner object.

Java Code Example

package com.javatutorialhq.tutorial;

import java.util.Scanner;

/*
 * Java example source code to convert String input to Boolean
 */

public class StringToBoolean {

	public static void main(String[] args) {
		System.out.print("Input:");
		Scanner scanner = new Scanner(System.in);

		// convert the string read from the scanner into Boolean data type
		Boolean input = Boolean.parseBoolean(scanner.nextLine());
		if (input) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}

		scanner.close();

	}

}

Sample Output

Running the above code would result for the following output

Input:true
YES

String to Boolean conversion in Java using valueOf

The following are the instructions on below example.

  • Generate a random number from 1 to 100.
  • Print the random number
  • Get the user input from Console using Scanner asking if he wants to print another random number
  • Convert the string input to Boolean object using valueOf
  • Use the boolean object for code logic wherein if the input is true the console will output another random number otherwise program terminates.

Java Code Example

package com.javatutorialhq.java.examples;

import java.util.Random;
import java.util.Scanner;

/*
 * This java example source code demonstrates how
 * to do conversion from String to Boolean
 * using valueOf()
 */

public class StringToBooleanExample {

	public static void main(String[] args) {
		
		Random random = new Random();
		// initialize minimum and maximum random number
		int min = 1;
		int max = 100;
		// declare initial value repeat
		Boolean repeat = true;
		// declare scanner object
		Scanner scan = new Scanner(System.in);
		do {
			// generate random integer
			int randInt = random.nextInt((max - min) + 1) + min;
			System.out.println(randInt);
			
			// ask user if he wants to generate random number
			System.out.print("Generate another (true/false)?");
			repeat = Boolean.valueOf(scan.nextLine());

		} while (repeat);

		// close scanner object
		scan.close();

	}

}

Sample Output

Running the above code will give the following console output

string to boolean conversion using valueof sample output

string to boolean conversion using valueof sample output