I
I
Ivan Dontsov2019-09-25 21:25:57
Java
Ivan Dontsov, 2019-09-25 21:25:57

How to convert string with value "0"/"1" to boolean value false/true?

Yes:
String strFalse = "0";
String strTrue = "1";
You need to convert the string to a boolean value, if 0 then false, if 1 then true.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-09-25
@Maksclub

import java.util.*;
import java.io.*;

public class Maksclub
{	
    public static String STRING_TRUE = "1";
    public static String STRING_FALSE = "0";

    public static void main (String[] args)
    {
        String strFalse = "0";
        String strTrue = "1";
        
        System.out.println(isTrueBinaryString(strFalse)); // false
        System.out.println(isTrueBinaryString(strTrue));  // true
        System.out.println(isTrueBinaryString(null));  // IllegalArgumentException
        System.out.println(isTrueBinaryString("5"));  // IllegalArgumentException
    }
  
    public static Boolean isTrueBinaryString(String value)
    {
        if (!STRING_FALSE.equals(value) && !STRING_TRUE.equals(value)) {
             throw new IllegalArgumentException("Value must be `0` or `1`.");
        }
      
        return STRING_TRUE.equals(value);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question