Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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!!
The problem is not to make it easily; the problem is to make is correctly.
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.
Richard MacCutchan 7-Oct-14 14:18pm
I disagree.
Will you please explain with example?
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?
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.

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:

String str="1234567";
       int sum=0;
       for(int i=1;i<str.length();i=i+2){

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

BufferedReader reader = new BufferedReader(new InputStreamReader(;
    System.out.print("Please enter number : ");
    String number=null;
        number = reader.readLine();
    catch (IOException e) {
        throw e;
    int sum=0;
    for(int i=1;i<number.length();i=i+2){

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
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:[^].

Maciej Los 12-Oct-14 14:27pm
Sergey Alexandrovich Kryukov 12-Oct-14 16:05pm
Thank you, Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900