1

I convert everything in my Android app into string and add it to a Sqlite database. I use the code below to convert boolean arrays into string, but I dont know how to convert it back from string into boolean array. There spaces between each true and false in the string. How can I break string at each space into a boolean array?

String work= "";
for (int i = 0;i<go.length; i++) {
    work= work+go[i];
    // Do not append comma at the end of last element
    if(i<go.length - 1){
        work = work+" ";
    }
}
3
  • Try googling you will find answer to all your question on previously asked questions on Stackoverflow. Commented May 28, 2012 at 10:54
  • what is go, is it string array? Commented May 28, 2012 at 10:56
  • oh sorry ya go is the string array Commented May 28, 2012 at 10:59

2 Answers 2

3
  1. Split the string on your separator character (" ")
  2. Create an array of booleans with the same length of the splitted array of strings
  3. Parse one by one them using Boolean.parseBoolean method

Example:

public static void main(String[] args) {

    String str = "true false true false false";

    String[] parts = str.split(" ");

    boolean[] array = new boolean[parts.length];
    for (int i = 0; i < parts.length; i++)
        array[i] = Boolean.parseBoolean(parts[i]);

    System.out.println(Arrays.toString(array));
}

Outputs:

[true, false, true, false, false]
Sign up to request clarification or add additional context in comments.

Comments

0

Please use boolean b = Boolean.parseBoolean(string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.