How to add all integer from String in sequence?
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
add a comment |
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
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
add a comment |
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
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
java
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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;
}
add a comment |
Try this:
int sum = store.chars()
.boxed()
.map(Character::getNumericValue)
.mapToInt(Integer::intValue)
.sum();
add a comment |
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.
add a comment |
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]);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 12 '18 at 14:10
Ishaan Javali
9271418
9271418
add a comment |
add a comment |
Try this:
int sum = store.chars()
.boxed()
.map(Character::getNumericValue)
.mapToInt(Integer::intValue)
.sum();
add a comment |
Try this:
int sum = store.chars()
.boxed()
.map(Character::getNumericValue)
.mapToInt(Integer::intValue)
.sum();
add a comment |
Try this:
int sum = store.chars()
.boxed()
.map(Character::getNumericValue)
.mapToInt(Integer::intValue)
.sum();
Try this:
int sum = store.chars()
.boxed()
.map(Character::getNumericValue)
.mapToInt(Integer::intValue)
.sum();
answered Nov 12 '18 at 14:16
ETO
1,708321
1,708321
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 12 '18 at 14:18
answered Nov 12 '18 at 14:09
forpas
8,7641421
8,7641421
add a comment |
add a comment |
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]);
add a comment |
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]);
add a comment |
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]);
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]);
edited Nov 12 '18 at 14:23
answered Nov 12 '18 at 14:10
Arnaud
13.4k21730
13.4k21730
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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