Click here to Skip to main content
15,867,686 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!!
Posted
Comments
Sergey Alexandrovich Kryukov 7-Oct-14 13:01pm    
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.
Sergey Alexandrovich Kryukov 7-Oct-14 13:11pm    
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.
Sergey Alexandrovich Kryukov 7-Oct-14 14:34pm    
Will you please explain with example?
—SA

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));
    System.out.print("Please enter number : ");
    String number=null;
    try{
        number = reader.readLine();
    }
    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
 
Share this answer
 
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
 
Share this answer
 
Comments
Maciej Los 12-Oct-14 14:27pm    
+5
Sergey Alexandrovich Kryukov 12-Oct-14 16:05pm    
Thank you, Maciej.
—SA

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