11

I have a Boolean array and I am trying to make a corresponding char array, so that to each true in the new array corresponds a 1 and for each false a 0. this is my code but it seems the new array is empty, because nothing prints, the Boolean nums[] prints fine.

char[] digits = new char[n];
for (int i = 0; i < n; i++) {
    if (nums[i]) {
        digits[i] = 1;
    }
    else if (!nums[i]) {
        digits[i] = 0;
    }
}
for (int k = 0; k < n; k++) {
    System.out.print (digits[k]);
}
9
  • 1
    First comment your else does not need if since if current index value is not true it will always be false Commented May 3, 2020 at 19:23
  • 1
    Also, there's no need to use nums[i] == true, just use nums[i], since that's a boolean itself Commented May 3, 2020 at 19:25
  • 2
    Also you could use conditional if directly digits[i]= nums[i] ? 1 : 0 and completely dump your if else blocks Commented May 3, 2020 at 19:25
  • What's the value of n? Can you show us the contents of digits? Commented May 3, 2020 at 19:26
  • 1
    @Youans: I wouldn't go for the ternary operator yet, he's clearly a beginner so it's better to start with more readabale code imo. Commented May 3, 2020 at 19:26

4 Answers 4

11

Your problem is that you don't have quotes surrounding the 1 and 0.

for (int i = 0; i < n; i++) {
    if (nums[i]) {
        digits[i] = '1';
    }
    else {
        digits[i] = '0';
    }
}

Without the quotes, they are cast from ints to chars. 0 is actually the null character (NUL), and 1 is start of heading or something like that. Java chars are encoded using UTF-16 (they're 16 bits long). The characters '0' and '1' are actually encoded by 48 and 49 respectively (in decimal).

EDIT: Actually, don't look at the ASCII table, look at the Unicode character set. Unicode is really a superset of ASCII, but it'll probably be more useful than the ascii table

Sign up to request clarification or add additional context in comments.

Comments

4

According to Primitive Data Types in the Language Basics lesson of trail Learning the Java Language in Oracle's Java tutorials:

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Unicode value 0 (zero) is a non-printing character, as is unicode value 1 (one). That's why you aren't seeing anything printed. Either change digits to a int array or fill it with character literals such as '0' or '1'

If you use int array, the following code will suffice:

int[] digits = new int[n];
for (int i=0; i<n; i++) {
    if (nums[i]) {
        digits[i] = 1;
    }
}

for (int k=0; k<n; k++) {
    System.out.print (digits[k]);
}

Note that a int array is implicitly initialized such that all the elements are initially 0 (zero).

Comments

3

you can do something like that

char[] myChars = new char[n/16];
for(int i=0;i<nums.length/16;i++);{
  String myChar ="";
  for(int j=0;j<16;j++){
     if(nums[i*16+j])
        myChar+="1";
     else
        myChar+="0";
  }
 myChars[i]=Integer.parseInt(myChar,2);
 }

Comments

3

You can convert like this:

    public static void main(String[] args) {
        int n = 5;
        boolean[] nums = { true, false, true, false, true };
        char[] digits = new char[n];
        for (int i = 0; i < n; i++) {
            digits[i] = nums[i] ? '1' : '0';
        }
    }

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.