0

My question seems very simple, but I've tried searching for a specific answer and have found none. I've found answers similar to what I've been looking for, but they've only managed to confuse me further:

All I want to do is add two character arrays and print the resulting array. The project deals with binary numbers, but I'll deal with base-2 arithmetic later, so just pretend they're base-10 numbers.

char[] array  = {'1', '0', '0', '1'};
char[] array2 = {'1', '1', '0', '0'};
char[] sum = new char[4];
for(i=0; i < 4; i++){
    sum[i] = char(array[i] + array2[i]);
    System.out.print(sum[i] + " ");
    }

My answer is "b''b" when I run it, so it seems some ASCII conversion is happening I guess? My expected answer should be "2101" and I realize the problem is in my casts, I just don't know how to proceed. Sum must remain a character array as part of the program's parameters.

EDIT: I KNOW using an int array for sum would solve this problem. As I stated in my original post, sum MUST remain a character array as part of the parameters of this project.

3
  • 1
    Why not use int[] instead? Commented Mar 12, 2014 at 5:50
  • 1
    Characters are not numbers, why are you expecting '1' + '1' = '2'? Commented Mar 12, 2014 at 5:50
  • sum[i]=char(array1[i])+char(array2[i]); Commented Mar 12, 2014 at 5:52

8 Answers 8

1

You can use Character.toString() to conver a char to String. Second, use Integer.parseInt() to convert a char to int. Then add those integers. Finally, use Character.forDigit(digit, 10) to convert the int (digit) to char.

char[] array = { '1', '0', '0', '1' };
char[] array2 = { '1', '1', '0', '0' };
char[] sum = new char[4];

for (int i = 0; i < 4; i++) {
    sum[i] = Character.forDigit(
            Integer.parseInt(Character.toString(array[i])) + Integer.parseInt(Character.toString(array2[i])),
            10);
    System.out.println(sum[i]);
}

Output:

2
1
0
1

Of course you can avoid this, if you use an array of integers:

int[] array = {1, 0, 0, 1};
Sign up to request clarification or add additional context in comments.

Comments

1

Your assumption that ASCII math is coming into play is correct. char variables are glorified int variables under the hood. Adding char variables basically takes the ASCII decimal value, adds those values and then gives you the ASCII character representation. This is a bit over-simplified, but I wanted to give you an idea of why this is happening.

For example, if '1' is decimal 49 in ASCII, '1' + '1' = 49 + 49 = 98. 98 in ASCII is 'b'.

I'd suggest just switching the type of arrays you are using to be int:

int[] array  = {1, 0, 0, 1};
int[] array2 = {1, 1, 0, 0};
int[] sum = new int[4];
for(int i = 0; i < 4; i++) {
    sum[i] = array[i] + array2[i];
    System.out.print(sum[i] + " ");
}

Comments

0

try to cast the character into int . something like

int i = Integer.parseInt(array[0] + "");

Similarly you can proceed for rest and then perform addition

Comments

0

If you want to add 1+1 and get 2 then don't use char array. Use int array, or cast it into int using

int i = Integer.parseInt(array[0] + "");

Comments

0

The problem is in your adding. Basically, what you'll need to do is sum the first character, array[i] + (array2[i] - '0'). This is because with the first character, you're getting a correct ASCII-0-indexed value, but the second one should just be a number of steps from the ASCII 0-index, so "- '0'", will give you just the difference.

Comments

0

Try this. It works. Change this sum variable from char[] array to int[] array

public static void main(String[] args) {
        char[] array = {'1', '0', '0', '1'};
        char[] array2 = {'1', '1', '0', '0'};
        int[] sum = new int[4];
        for (int i = 0; i < 4; i++) {
            sum[i] = Integer.parseInt(array[i] + "") + Integer.parseInt(array2[i] + "");
            System.out.print(sum[i] + " ");
        }
    }

Output

run: 2 1 0 1 BUILD SUCCESSFUL (total time: 0 seconds)

Comments

0

Use int array instead of char array and store the result into your sum char array. But use int array as we'll for storing result. This will help you in dealing with numerical operation

Comments

0

I think this sounds like a question asked in interviews. The intention of the question is to add Integer characters from two arrays and output the result as an array of characters.

    char[] a = { '4', '5', '6' };
    char[] b = { '7', '8', '9' };
    Integer c = Integer.parseInt(String.valueOf(a)) + Integer.parseInt(String.valueOf(b)); //456 + 789 = 1245
    final char[] d = c.toString().toCharArray(); // d = {'1',2,'4','5'}

This solution handles all the cases where the sum can end up with a carry.

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.