How to add all integer from String in sequence?












0














for Example:



2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26



but i does not get desired results



when i add all char it gives
output as:
266



import java.util.Scanner;

public class ProjectEu {
public static void main(String...rDX) {
int degree = new Scanner(System.in).nextInt();
String store = Integer.toString((int)Math.pow(2,degree));
char finals = store.toCharArray();

int temp = 0;
for (int i = 0, n = store.length(); i < n; i++) {
System.out.printf("values[%d] --> %c n",i, finals[i]);
temp = temp + finals[i];
}

System.out.println(temp);
}
}









share|improve this question
























  • Pratik did my answer help you?
    – Ishaan Javali
    Nov 23 '18 at 18:38










  • Pratik Katariya, if my answer helped you can you please mark it as accepted?
    – Ishaan Javali
    Dec 20 '18 at 20:13


















0














for Example:



2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26



but i does not get desired results



when i add all char it gives
output as:
266



import java.util.Scanner;

public class ProjectEu {
public static void main(String...rDX) {
int degree = new Scanner(System.in).nextInt();
String store = Integer.toString((int)Math.pow(2,degree));
char finals = store.toCharArray();

int temp = 0;
for (int i = 0, n = store.length(); i < n; i++) {
System.out.printf("values[%d] --> %c n",i, finals[i]);
temp = temp + finals[i];
}

System.out.println(temp);
}
}









share|improve this question
























  • Pratik did my answer help you?
    – Ishaan Javali
    Nov 23 '18 at 18:38










  • Pratik Katariya, if my answer helped you can you please mark it as accepted?
    – Ishaan Javali
    Dec 20 '18 at 20:13
















0












0








0







for Example:



2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26



but i does not get desired results



when i add all char it gives
output as:
266



import java.util.Scanner;

public class ProjectEu {
public static void main(String...rDX) {
int degree = new Scanner(System.in).nextInt();
String store = Integer.toString((int)Math.pow(2,degree));
char finals = store.toCharArray();

int temp = 0;
for (int i = 0, n = store.length(); i < n; i++) {
System.out.printf("values[%d] --> %c n",i, finals[i]);
temp = temp + finals[i];
}

System.out.println(temp);
}
}









share|improve this question















for Example:



2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26



but i does not get desired results



when i add all char it gives
output as:
266



import java.util.Scanner;

public class ProjectEu {
public static void main(String...rDX) {
int degree = new Scanner(System.in).nextInt();
String store = Integer.toString((int)Math.pow(2,degree));
char finals = store.toCharArray();

int temp = 0;
for (int i = 0, n = store.length(); i < n; i++) {
System.out.printf("values[%d] --> %c n",i, finals[i]);
temp = temp + finals[i];
}

System.out.println(temp);
}
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 15:01









Oussema Aroua

4,02611634




4,02611634










asked Nov 12 '18 at 14:03









Pratik Katariya

111




111












  • Pratik did my answer help you?
    – Ishaan Javali
    Nov 23 '18 at 18:38










  • Pratik Katariya, if my answer helped you can you please mark it as accepted?
    – Ishaan Javali
    Dec 20 '18 at 20:13




















  • Pratik did my answer help you?
    – Ishaan Javali
    Nov 23 '18 at 18:38










  • Pratik Katariya, if my answer helped you can you please mark it as accepted?
    – Ishaan Javali
    Dec 20 '18 at 20:13


















Pratik did my answer help you?
– Ishaan Javali
Nov 23 '18 at 18:38




Pratik did my answer help you?
– Ishaan Javali
Nov 23 '18 at 18:38












Pratik Katariya, if my answer helped you can you please mark it as accepted?
– Ishaan Javali
Dec 20 '18 at 20:13






Pratik Katariya, if my answer helped you can you please mark it as accepted?
– Ishaan Javali
Dec 20 '18 at 20:13














4 Answers
4






active

oldest

votes


















1














The reason that you are getting this error is because temp is an integer, but finals[i] is a character, so it converts the characters into ASCII values and adds them. You can fix this problem by doing:



for (int i = 0, n = store.length(); i < n; i++) {
char ch = store.charAt(i);
int digit = Integer.parseInt(Character.toString(ch));
temp = temp + digits;
}





share|improve this answer





























    0














    Try this:



    int sum = store.chars()
    .boxed()
    .map(Character::getNumericValue)
    .mapToInt(Integer::intValue)
    .sum();





    share|improve this answer





























      0














      This line:



      temp = temp + finals[i];


      sums temp and the ASCII code of the char stored in finals[i].

      You can get the value of the digit by this:



      temp = temp + finals[i] - '0';


      This means that by subtracting from the digit's ASCII code the ASCII code of 0 you get the number value of the digit.






      share|improve this answer































        0














        When you are adding a character to an integer, you are adding the integer code of the character, not its actual numeric value .



        What you need is Character.getNumericValue :



        temp = temp + Character.getNumericValue(finals[i]);





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263800%2fhow-to-add-all-integer-from-string-in-sequence%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The reason that you are getting this error is because temp is an integer, but finals[i] is a character, so it converts the characters into ASCII values and adds them. You can fix this problem by doing:



          for (int i = 0, n = store.length(); i < n; i++) {
          char ch = store.charAt(i);
          int digit = Integer.parseInt(Character.toString(ch));
          temp = temp + digits;
          }





          share|improve this answer


























            1














            The reason that you are getting this error is because temp is an integer, but finals[i] is a character, so it converts the characters into ASCII values and adds them. You can fix this problem by doing:



            for (int i = 0, n = store.length(); i < n; i++) {
            char ch = store.charAt(i);
            int digit = Integer.parseInt(Character.toString(ch));
            temp = temp + digits;
            }





            share|improve this answer
























              1












              1








              1






              The reason that you are getting this error is because temp is an integer, but finals[i] is a character, so it converts the characters into ASCII values and adds them. You can fix this problem by doing:



              for (int i = 0, n = store.length(); i < n; i++) {
              char ch = store.charAt(i);
              int digit = Integer.parseInt(Character.toString(ch));
              temp = temp + digits;
              }





              share|improve this answer












              The reason that you are getting this error is because temp is an integer, but finals[i] is a character, so it converts the characters into ASCII values and adds them. You can fix this problem by doing:



              for (int i = 0, n = store.length(); i < n; i++) {
              char ch = store.charAt(i);
              int digit = Integer.parseInt(Character.toString(ch));
              temp = temp + digits;
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 '18 at 14:10









              Ishaan Javali

              9271418




              9271418

























                  0














                  Try this:



                  int sum = store.chars()
                  .boxed()
                  .map(Character::getNumericValue)
                  .mapToInt(Integer::intValue)
                  .sum();





                  share|improve this answer


























                    0














                    Try this:



                    int sum = store.chars()
                    .boxed()
                    .map(Character::getNumericValue)
                    .mapToInt(Integer::intValue)
                    .sum();





                    share|improve this answer
























                      0












                      0








                      0






                      Try this:



                      int sum = store.chars()
                      .boxed()
                      .map(Character::getNumericValue)
                      .mapToInt(Integer::intValue)
                      .sum();





                      share|improve this answer












                      Try this:



                      int sum = store.chars()
                      .boxed()
                      .map(Character::getNumericValue)
                      .mapToInt(Integer::intValue)
                      .sum();






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 12 '18 at 14:16









                      ETO

                      1,708321




                      1,708321























                          0














                          This line:



                          temp = temp + finals[i];


                          sums temp and the ASCII code of the char stored in finals[i].

                          You can get the value of the digit by this:



                          temp = temp + finals[i] - '0';


                          This means that by subtracting from the digit's ASCII code the ASCII code of 0 you get the number value of the digit.






                          share|improve this answer




























                            0














                            This line:



                            temp = temp + finals[i];


                            sums temp and the ASCII code of the char stored in finals[i].

                            You can get the value of the digit by this:



                            temp = temp + finals[i] - '0';


                            This means that by subtracting from the digit's ASCII code the ASCII code of 0 you get the number value of the digit.






                            share|improve this answer


























                              0












                              0








                              0






                              This line:



                              temp = temp + finals[i];


                              sums temp and the ASCII code of the char stored in finals[i].

                              You can get the value of the digit by this:



                              temp = temp + finals[i] - '0';


                              This means that by subtracting from the digit's ASCII code the ASCII code of 0 you get the number value of the digit.






                              share|improve this answer














                              This line:



                              temp = temp + finals[i];


                              sums temp and the ASCII code of the char stored in finals[i].

                              You can get the value of the digit by this:



                              temp = temp + finals[i] - '0';


                              This means that by subtracting from the digit's ASCII code the ASCII code of 0 you get the number value of the digit.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 12 '18 at 14:18

























                              answered Nov 12 '18 at 14:09









                              forpas

                              8,7641421




                              8,7641421























                                  0














                                  When you are adding a character to an integer, you are adding the integer code of the character, not its actual numeric value .



                                  What you need is Character.getNumericValue :



                                  temp = temp + Character.getNumericValue(finals[i]);





                                  share|improve this answer




























                                    0














                                    When you are adding a character to an integer, you are adding the integer code of the character, not its actual numeric value .



                                    What you need is Character.getNumericValue :



                                    temp = temp + Character.getNumericValue(finals[i]);





                                    share|improve this answer


























                                      0












                                      0








                                      0






                                      When you are adding a character to an integer, you are adding the integer code of the character, not its actual numeric value .



                                      What you need is Character.getNumericValue :



                                      temp = temp + Character.getNumericValue(finals[i]);





                                      share|improve this answer














                                      When you are adding a character to an integer, you are adding the integer code of the character, not its actual numeric value .



                                      What you need is Character.getNumericValue :



                                      temp = temp + Character.getNumericValue(finals[i]);






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 12 '18 at 14:23

























                                      answered Nov 12 '18 at 14:10









                                      Arnaud

                                      13.4k21730




                                      13.4k21730






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263800%2fhow-to-add-all-integer-from-string-in-sequence%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          The Sandy Post

                                          Danny Elfman

                                          Pages that link to "Head v. Amoskeag Manufacturing Co."