Converts a String of 0's and 1's to a boolean[]. - Java java.lang

Java examples for java.lang:boolean

Description

Converts a String of 0's and 1's to a boolean[].

Demo Code

/* Copyright (c) 2002 The European Commission DREAM Project IST-1999-12679
 *
 * This file is part of JEO.//from ww  w . j  a v a2s.c o m
 * JEO is free software; you can redistribute it and/or modify it under the terms of GNU 
 * General Public License as published by the Free Sortware Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JEO is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 * License for mor details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 59 TEmple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String pString = "java2s.com";
        System.out
                .println(java.util.Arrays.toString(stringToBits(pString)));
    }

    /**
     * Converts a String of 0's and 1's to a boolean[]. 
     * @param pString string of 0's and 1's
     * @return boolean[] of length pString.length() which has true for each index which is non-zero in the String
     */
    public static boolean[] stringToBits(String pString) {
        boolean[] vBits = new boolean[pString.length()];
        for (int i = 0; i < vBits.length; i++) {
            vBits[i] = pString.charAt(i) != '0';
        }

        return vBits;
    }
}

Related Tutorials