Tyler Rose
Tyler Rose

Reputation: 157

Java int value type to Character

I am new to java and and working on a crud calculator that takes input and holds it in an ArrayList to perform the calculations.

I am trying to add two values in an ArrayList<Character> and then replace the "+" with the sum.

    if(listEqu.contains('+')) {
        while(listEqu.indexOf('+') > -1) {

                int plus = listEqu.indexOf('+');
                int prev = listEqu.get(plus-1);
                int nxt = listEqu.get(plus+1);
                Character sum = (char) (nxt + prev);

                listEqu.set(plus, sum);

                System.out.println(listEqu);

        }
    }

When the input is 1+1, this returns [1, b, 1]. What I want is to return [1, 2, 1] . Any advice? Thanks!

Upvotes: 1

Views: 378

Answers (5)

Stephen C
Stephen C

Reputation: 719229

The problem is actually that adding two characters doesn't do what you expect.

The value of '1' + '1' is 'b'. If you want the next digit after '1' you add the integer 1 to it; i.e. '1' + 1 is '2'.

For a deeper understanding, you need to understand how character data is represented in Java.

Each char value in Java is an unsigned 16 bit integer that corresponds to a code point (or character code) in the Unicode basic plane. The first 128 of these code points (0 to 127) correspond to a characters in the old ASCII character set. In ASCII the codes that represent digits are 48 (for '0') through to 39 (for '9'). And the lowercase letters are 97 (for 'a') through to 122 (for 'z').

So as you can see, '1' + '1' -> 49 + 49 -> 98 -> 'b'.

(In fact there is a lot more to it than this. Not all char values represent real characters, and some Unicode code-points require two char values. But this is way beyond the scope of your question.)


How could I specify addition of numbers instead of addition of the characters?

You convert the character (digit) to a number, perform the arithmetic, and convert the result back to a character.

Read the javadoc for the Character class; e.g. the methods Character.digit and Character.forDigit.

Note that this only works while the numbers remain in the range 0 through 9. For a number outside of that range, the character representation consists of two or more characters. For those you should be using String rather than char. (A String also copes with the 1 digit case too ...)

Upvotes: 1

Naman
Naman

Reputation: 31968

Few things that can be improved with your code :

  1. Converting the characters 1 into equivalent integer value:

    int prev = Integer.parseInt(String.valueOf(listEqu.get(plus-1)));
    int nxt = Integer.parseInt(String.valueOf(listEqu.get(plus+1)));
    // Note : int prev = listEqu.get(plus-1) would store an ascii value of `1` to prev value i.e 49
    
  2. And then converting the sum of those two values into Character back to be added to the list using Character.forDigit as:

    Character sum = Character.forDigit(nxt+prev,10);
    // Note Character sum = (char) (nxt + prev); is inconvertible
    // and char sum = (char) (nxt + prev); would store character with ascii value 98(49+49) in your case 'b' to sum
    

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201467

You need to parse the characters to their corresponding decimal value before you perform the addition, and then back to a character after. The methods Character.digit(char, int) and Character.forDigit(int, int) can do that (and I would use char since that is the type of prev and nxt). Like,

char prev = listEqu.get(plus - 1);
char nxt = listEqu.get(plus + 1);
Character sum = Character.forDigit(Character.digit(nxt, 10) 
        + Character.digit(prev, 10), 10);

Upvotes: 0

Khoyo
Khoyo

Reputation: 1247

nxt and prev are char values. Tey take their value in the ASCII table, where '1' is 61 and 'b' is 142 (thus, '1' + '1' = 'b')

You need to substract '0' to get the number they represent. ('1' - '0' = 61 - 60 = 1)

The sum is not necessarily writable with one character, so you shouldn't put it back into a char array.

If you want to convert an integer to a string, use Integer.toString(i).

(And, if you want to, get the first character of the string and put it in the array, if that's what you want)

Upvotes: 0

pouyan
pouyan

Reputation: 3449

you should first convert your prevand nxt to int value and then add them together like follow:

if(listEqu.contains('+')) {
    while(listEqu.indexOf('+') > -1) {

            int plus = listEqu.indexOf('+');
            int prev = Integer.parseInt(listEqu.get(plus-1));
            int nxt = Integer.parseInt(listEqu.get(plus+1));
            Character sum = (char) (nxt + prev);

            listEqu.set(plus, sum);

            System.out.println(listEqu);

    }
}

Upvotes: 0

Related Questions