English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The importance of the parseBoolean() method in Java

The parseBoolean() method is an important method of the Boolean class. parseBoolean() is a static method that can parse a string argument into a Boolean object. The parseBoolean() method of the Boolean class returns the boolean value represented by the string argument.

Grammar

public static boolean parseBoolean(String s)}

Example

import java.util.Scanner;
public class ParseBooleanMethodTest {
   public static void main(String[] args) {
      System.out.print("Are you ready to play cricket(true/false)? ");
      Scanner scanner = new Scanner(System.in);
      String str = scanner.nextLine();
      scanner.close();      //Convert user input to boolean value 
      boolean answer = Boolean.parseBoolean(str);
      if(answer) {
         System.out.println("Yes, I am ready to play cricket");
      } else {
         System.out.println("No, I am not yet ready");
      }
   }
}

Output Result

Are you ready to play cricket(true/false)? true
Yes, I am ready to play cricket