1

I am fairly new to java, about 3 weeks into my course. In my assignment, I needed to use charAt to separate the string into 14 parts. Now I need to use addition and add these together.

I have tried many times with no success. Every time I add them together and print it out it gives me a number way bigger than it should be.

char num1 = roulette.charAt(0);
char num2 = roulette.charAt(1);
char num3 = roulette.charAt(2);
char num4 = roulette.charAt(3);
char num5 = roulette.charAt(4);
char num6 = roulette.charAt(5);

When I add num1+num2+num3+num4+num5+num6, I get a number way bigger than it should be.

Am I missing something?

AntonH
  • 6,359
  • 2
  • 30
  • 40
Survivorr
  • 83
  • 3
  • 10
  • please provide your full code. – Wasi Ahmad Feb 15 '17 at 21:07
  • 1
    If you're adding the characters, you're adding the (ASCII) values of the characters, not the number that it represents. – AntonH Feb 15 '17 at 21:07
  • https://stackoverflow.com/questions/4968323/java-parse-int-value-from-a-char – AntonH Feb 15 '17 at 21:12
  • @AntonH You mean the UTF-16 code unit, not ASCII. For example, `'9'` plus `'9'` becomes 114, which is the complete UTF-16 encoding for [`'r'`](http://www.fileformat.info/info/unicode/char/0072/index.htm). – Tom Blodget Feb 16 '17 at 00:10
  • @TomBlodget Yeah, I was blanking on the name, so put ASCII instead. – AntonH Feb 16 '17 at 01:51

4 Answers4

1

This is due to you adding the characters together, they will not turn into the number equivalent automatically. You will need to change them yourself, to do this you can use Integer.parseInt(char) and you can add them together like that. For example Integer.parseInt(String.valueOf('1') + Integer.parseInt(String.valueOf('2')) this will add 1 + 2 together correctly now resulting in 3 rather than appending 2 to the 1 making 12

Walshy
  • 850
  • 2
  • 11
  • 32
0

You cannot cast your chars to integer first try like this

Integer.parseInt(num1) + Integer.parseInt(num2) +Integer.parseInt(num3)...

and so on.

EDIT

I just learned that you cannot use Integer.parseInt(num1) for Character. You should cast your chars as below:

char a = '5';
int b = Integer.parseInt(String.valueOf(a));

int c=b+b;
System.out.println(c); //this will give 10
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Ali insan Soyaslan
  • 836
  • 5
  • 14
  • 33
0

If you add characters to characters, it means you are adding their ascii values. But if you want to add the numeric value which is presented as a Character in the String, then you have to convert the character to integer first. See the example given below.

N.B. when you want to add a sequence of values, use loop.

Example

String roulette = "123456";
int sum = 0;
for (int i = 0; i < roulette.length(); i++) {
    sum = sum + roulette.charAt(i);
}
System.out.println("Sum : " + sum);

sum = 0;
for (int i = 0; i < roulette.length(); i++) {
    sum = sum + Integer.parseInt(String.valueOf(roulette.charAt(i)));
}
System.out.println("Sum : " + sum);

Output

Sum : 309
Sum : 21

Case 1: sum = sum + roulette.charAt(i);

Adding ascii values of the numbers. So the sum is 309.

ascii_value('1') - 49
ascii_value('2') - 50
...
ascii_value('5') - 53
ascii_value('6') - 54

Sum = 49 + 50 + 51 + 52 + 53 + 54 = 309

Case 2: sum = sum + Integer.parseInt(String.valueOf(roulette.charAt(i)));

Adding the numeric value instead of the ascii values. So the sum is 21.

Sum = 1 + 2 + 3 + 4 + 5 + 6 = 21
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
0

If you want to add the characters then each character has a character code that will be used. so for example according to the ASCII Table 'a' = 97, 'b' = 98, 'c' = 99; so if you add these together you will get 294. ASCII Table https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html.

However, if each character represents a number and you want to add the numbers then you can do something like this:

char num1 = roulette.charAt(0);
int firstNum = Integer.parseInt(Character.toString(num1));

char num2 = roulette.charAt(1);
int secondNum = Integer.parseInt(Character.toString(num2));

char num3 = roulette.charAt(2);
int thirdNum = Integer.parseInt(Character.toString(num3));

char num4 = roulette.charAt(3);
int fourthNum = Integer.parseInt(Character.toString(num4));

char num5 = roulette.charAt(4);
int fifthNum = Integer.parseInt(Character.toString(num5));

char num6 = roulette.charAt(5);
int sixthNum = Integer.parseInt(Character.toString(num6));

int result = firstNum + secondNum + thirdNum + fourthNum + fifthNum + sixthNum;
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126