Home > Java > javaTutorial > What is the importance of parseBoolean() method in Java?

What is the importance of parseBoolean() method in Java?

WBOY
Release: 2023-08-28 15:29:03
forward
1280 people have browsed it

What is the importance of parseBoolean() method in Java?

parseBoolean() method is an important method of the Boolean class. parseBoolean() is a static method that parses String method parameters into Boolean objects. The parseBoolean() method of the Boolean class returns the Boolean value represented by the string argument.

Syntax

public static boolean parseBoolean(String s)
Copy after login

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();
<strong>      //</strong> Convert the user input into boolean<strong>
</strong>      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");
      }
   }
}
Copy after login

Output

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

The above is the detailed content of What is the importance of parseBoolean() method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template