14,970,251 members
See more:
Working on an assignment that will take a word and display the sum of the numbers comprising its character's unicode values.

I am thinking about declaring and initialize the variable of some of the characters like hello d1=h...d2=e...and so on. then convert character to int, but is there an easier route to do? Please help! Thanks!!
Posted

The problem is not to make it easily; the problem is to make is correctly.
—SA
Richard MacCutchan 7-Oct-14 13:03pm

No conversion necessary, each character already has a numeric value, just add them together.

This is potentially incorrect, because it depends on UTF; and the API is supposed to be transparent to particular bit representation of characters. Please see my answer for further detail.
—SA
Richard MacCutchan 7-Oct-14 14:18pm

I disagree.

Will you please explain with example?
—SA
Richard MacCutchan 7-Oct-14 14:39pm

I don't need to. As usual you are making assumptions about what is in the questioners mind and telling me I'm wrong because you know better than him what he is asking for.

You don't need to, but I'm just asking you for a favor to explain my mistake. If my assumption was wrong, I would like to know where. Will you please?
—SA
Maciej Los 12-Oct-14 14:27pm

Richard, i'am interested your opinion too. As per my experience, Sergey never tell me i'm wrong. We always discuss on arguments.
Member 11135824 7-Oct-14 14:37pm

can you show me an example?
Richard MacCutchan 7-Oct-14 14:41pm

Just take each character in turn, save it as an integer and add that value to your total.

## Solution 2

From your question what i have deduced that, you want to add up all numbers in the word.Here is how you can achieve that:

You can just get the string input and then , use a for loop to find sum of all of its characters and then finally print it , here is the simple logic for the same:

C#
```String str="1234567";
int sum=0;
for(int i=1;i<str.length();i=i+2){
sum+=(str.charAt(i)-'0');
}
System.out.println(sum);```

You can take input from user for the number and can do the same

C#
```BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String number=null;
try{
}
catch (IOException e) {
e.printStackTrace();
throw e;
}
int sum=0;
for(int i=1;i<number.length();i=i+2){
sum+=(number.charAt(i)-'0');
}
System.out.println(sum);```

In both cases you will get the sum of the numbers. This is both easy and correct.

No huzz no fuzz.. and you kill the buzz.. :P

I hope that will help.

Best Regards

## Solution 1

The problem is not as simple as it may seem. Just typecasting to `int` is supposed to work but is ideologically incorrect. The support of Unicode in languages and platform API's should be transparent: it should not rely on the knowledge of any particular UTF which is internally used by one or another platform (such as JVM or the OS each implementation is based on).

Also, in most UTFs (except 32-bit ones), the character takes different number of bytes. Say, in UTF16LE, most practically used characters are represented by a 16-bit words, but some are above the BMP (base plane which fits in 16-bit representation) uses two such words (called surrogage pairs). If you assume some particular bitwise representation of characters, you can get correct results for some characters and wrong for others.

So, this aspect should be delegated to API. In Java, such API is `codePointAt` and other methods named "*codePoint*" or related to code point parameters. You can find all of them here: http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#codePointAt-char:A-int-[^].

—SA
Maciej Los 12-Oct-14 14:27pm

+5
Sergey Alexandrovich Kryukov 12-Oct-14 16:05pm

Thank you, Maciej.
—SA