Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Checking Code: Getting the Unicode value of each character from a string.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Checking Code: Getting the Unicode value of each character from a string.

    /* Something is wrong here not exactly sure i am a beginner. I cannot use ANY classes.
    EXAMPLE: if argument value is "ABC" then the return value should be "198".
    Help! Thanks in advance*/

    public class Test {

    public static void main(String[] args) {
    addChars("ABC");
    }
    public static int addChars ( String s ) {
    int sum = 0;
    for (int i=0; i<s.length();i++) {
    if(s.charAt(i) <= s.length()) {
    sum += (int)s.charAt(i); // adds Unicode value to sum

    }
    else {
    return 0;
    }
    }
    return sum;
    }
    }


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Checking Code: Getting the Unicode value of each character from a string.

    I'm not entirely sure what you mean, but have a look at what this line is doing, is it ever true?

    if(s.charAt(i) <= s.length())

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Checking Code: Getting the Unicode value of each character from a string.

    Well every character has value i have to produce that value. I would believe that it is true. My string is "ABC" so as i access the string with s.charAt(i) grabbing that first character 'A' it is less than or equal too the length of the String.

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. How to convert or break String into Character
    By Bharath12 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 14th, 2011, 11:41 PM
  3. Extracting the " (Double Quote) character from a string
    By jai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 28th, 2011, 08:57 AM
  4. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM

Tags for this Thread