Home > Mobile >  Adding two numbers stored as arrays of chars
Adding two numbers stored as arrays of chars

Time:05-20

I'm trying to write an algorithm which adds two numbers that are stored as chars in two arrays. Unfortunately, it doesn't work. When I try to debug it, I see that the variables a and b get the value -1 which makes no sense. Any idea what might be the problem?

public class rechner2 {

    public static void main(String[] args) {
        final char[] zahl1 = {1, 2, 3};
        final char[] zahl2 = {7, 8, 9};
        
        //Add arrays zahl1 and zahl2.
        char [] zwischenarray = add(zahl1, zahl2);
        for (int i = 0; i < zwischenarray.length; i  ) {
            System.out.println(zwischenarray[i]);
        }
    }
    

    private static char[] add(char[] zahl1, char[] zahl2) {
        int len;
        if (zahl1.length < zahl2.length) {
            len = zahl2.length;
        } else {
            len = zahl1.length;
        }
            
        char[] finalresult = new char [len   1];
        int carryover = 0;
        
        for (int i = 0; i < len; i  ) {
            int a = Character.getNumericValue(zahl1[i]);            
            int b = Character.getNumericValue(zahl2[i]);
            int c = a   b   carryover;
            if (c > 9) {
                carryover = 1;
                c = c - 10;
            } else {
                carryover = 0;
            }
            finalresult[i] = (char)c;
        }
        
        if (carryover == 1) {
            finalresult[len   1] = 1;
        }
        
        return finalresult;
    }
}

CodePudding user response:

in this code I believe 2 bug

  1. instead of char , i guess better to us int
  2. length of the array

here is the code: public class rechner2 {

public static void main(String[] args) {
     int[] zahl1 = {1,2,3};
     int[] zahl2 = {7,8,9};

    //Add arrays zahl1 and zahl2.
    int [] zwischenarray = add(zahl1, zahl2);
    for (int i = 0; i < zwischenarray.length; i  ) {
        System.out.println(zwischenarray[i]);
    }
}


private static int[] add(int[] zahl1, int[] zahl2) {
    int len;
    if (zahl1.length < zahl2.length) {
        len = zahl2.length;
    } else {
        len = zahl1.length;
    }

    int[] finalresult = new int [len   1];
    int carryover = 0;

    for (int i = 0; i <= len-1; i  ) {
        int a = (zahl1[i]);
        int b = (zahl2[i]);
        int c = a   b   carryover;
        if (c > 9) {
            carryover = 1;
            c = c - 10;
        } else {
            carryover = 0;
        }
        finalresult[i] = c;
    }

    if (carryover == 1) {
        finalresult[len] = 1;
    }

    return finalresult;
}

}

CodePudding user response:

char foo = '3'; is different than char foo = 3;.

In the former, the three is what you get when you type the "3" key. That's equivalent to ASCII "51" in decimal, "33" in base sixteen, or "0011 0011" in binary. It's a "printable" character.

The latter, on the other hand, is equivalent to "11" in binary. It's like short foo = 3. Except it's a char instead. As a character, it's a "non-printable" character. It's equivalent to ASCII control character, ETX.

Your code is conflicted: Most of your code is treating your char data as one of the integer data types. But, some of it is treating the data as "printable characters".

Character.getNumericalValue (char ch) is returning -1 because the values in zahl1 and zahl2 are outside the range of char values it was intended to work with.

Here is a demonstration. Change

    int a = Character.getNumericValue(zahl1[i]);            
    int b = Character.getNumericValue(zahl2[i]);
    int c = a   b   carryover;

to

    int c =  zahl1[i]   zahl2[i]   carryover;

This shows you can do arithmetic on char types. But, to see the result, you need to include a cast in your output:

   System.out.println((short) zwischenarray[i]);

You can change occurrences of char to short or int in your code and get the same results.

You will quickly find you still have a bug. But, my purpose here was to demonstrate that you are treating char as short.

  • Related