Results 1 to 5 of 5
  1. #1
    greg677 is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default add ascii values together

    hey guys im trying to find a way to add all the ascii values the are outputted by this loop

    Java Code:
       for (int i=0; i < word.length(); i++)
          {
          char c = word.charAt(i);
          int j = (int) c;
          System.out.println("ASCII of: "+ c +" = " + j);
          }
    at the moment each individual ascii value is outputted to the screen, but i want it to add all the ascii values together in one integer. how do i do this? Thanks for the help

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by greg677 View Post
    hey guys im trying to find a way to add all the ascii values the are outputted by this loop

    Java Code:
       for (int i=0; i < word.length(); i++)
          {
          char c = word.charAt(i);
          int j = (int) c;
          System.out.println("ASCII of: "+ c +" = " + j);
          }
    at the moment each individual ascii value is outputted to the screen, but i want it to add all the ascii values together in one integer. how do i do this? Thanks for the help
    It's nothing more than adding all those values of your local variable j to another variable (initialized to 0) and use that value when the loop has finished ...

    kind regards,

    Jos

  3. #3
    greg677 is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Yes that is what i am trying to do but im not sure how to type it up, thanks for the help

  4. #4
    gbtimmon is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
       int total = 0;
       for (int i=0; i < word.length(); i++)
          {
          char c = word.charAt(i);
          int j = (int) c;
          total += c;
          System.out.println("ASCII of: "+ c +" = " + j);
          }
          System.out.println(total);
    it is a very simple problem.
    here you go

    the
    total += c;
    function i used translates in java to
    total = total + c;
    useful to know.

  5. #5
    greg677 is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    ahh cool thanks i was doing it like total =+ c; lol the wrong way

Similar Threads

  1. Binary and ASCII
    By mac in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 06:31 PM
  2. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  3. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  4. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  5. Printing ASCII values of characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •