JAVA Max Value from a String that contains only digits
I have some problem with an exercise from geeksforgeeks portal. I don't know why they say to me wrong answer. I have completed the function and my results on their visible test cases is good. Maybe i don't understand what they want from me to solve in the problem? Can you help me please?
The problem is this 
My code is this:
//Your Code here
long answer = 0;
for (int i = 0; i < str.length(); i++)
{
long multiply = (str.charAt(i) - 48) * answer;
long sum = (str.charAt(i) - 48) + answer;
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
}
System.out.println(answer);
And as you can see... my solution works for the visible test cases:

Important!!! They don't show the input test cases where the code have failed. They don't show any input of the test cases.
EDITED:
some request of custom input to show (also changed a little the if else statement... included now the possibility of equals sum and multiply

java
|
show 3 more comments
I have some problem with an exercise from geeksforgeeks portal. I don't know why they say to me wrong answer. I have completed the function and my results on their visible test cases is good. Maybe i don't understand what they want from me to solve in the problem? Can you help me please?
The problem is this 
My code is this:
//Your Code here
long answer = 0;
for (int i = 0; i < str.length(); i++)
{
long multiply = (str.charAt(i) - 48) * answer;
long sum = (str.charAt(i) - 48) + answer;
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
}
System.out.println(answer);
And as you can see... my solution works for the visible test cases:

Important!!! They don't show the input test cases where the code have failed. They don't show any input of the test cases.
EDITED:
some request of custom input to show (also changed a little the if else statement... included now the possibility of equals sum and multiply

java
Maybe your code is failing one (or more) of their secret tests?
– Joe C
Nov 25 '18 at 15:29
I am voting to close this question as off-topic because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Joe C
Nov 25 '18 at 15:30
why to close my question? I'm just asking for some help... I tried by myself all possible solutions and now I'm with no option to test and try (no chance to solve this) ... I need little help from other people cause I don't see what I am doing wrong? :(((
– Cristian Babarusi
Nov 25 '18 at 16:02
We expect to see an input which reproduces your issue, the expected output, why this output is expected, and the actual output. You have provided none of these things.
– Joe C
Nov 25 '18 at 16:07
i provided in question pictures with input ... but i will put another picture with some of custom inputs and outputs of my code...
– Cristian Babarusi
Nov 25 '18 at 16:12
|
show 3 more comments
I have some problem with an exercise from geeksforgeeks portal. I don't know why they say to me wrong answer. I have completed the function and my results on their visible test cases is good. Maybe i don't understand what they want from me to solve in the problem? Can you help me please?
The problem is this 
My code is this:
//Your Code here
long answer = 0;
for (int i = 0; i < str.length(); i++)
{
long multiply = (str.charAt(i) - 48) * answer;
long sum = (str.charAt(i) - 48) + answer;
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
}
System.out.println(answer);
And as you can see... my solution works for the visible test cases:

Important!!! They don't show the input test cases where the code have failed. They don't show any input of the test cases.
EDITED:
some request of custom input to show (also changed a little the if else statement... included now the possibility of equals sum and multiply

java
I have some problem with an exercise from geeksforgeeks portal. I don't know why they say to me wrong answer. I have completed the function and my results on their visible test cases is good. Maybe i don't understand what they want from me to solve in the problem? Can you help me please?
The problem is this 
My code is this:
//Your Code here
long answer = 0;
for (int i = 0; i < str.length(); i++)
{
long multiply = (str.charAt(i) - 48) * answer;
long sum = (str.charAt(i) - 48) + answer;
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
}
System.out.println(answer);
And as you can see... my solution works for the visible test cases:

Important!!! They don't show the input test cases where the code have failed. They don't show any input of the test cases.
EDITED:
some request of custom input to show (also changed a little the if else statement... included now the possibility of equals sum and multiply

java
java
edited Nov 25 '18 at 16:19
Cristian Babarusi
asked Nov 25 '18 at 15:09
Cristian BabarusiCristian Babarusi
519516
519516
Maybe your code is failing one (or more) of their secret tests?
– Joe C
Nov 25 '18 at 15:29
I am voting to close this question as off-topic because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Joe C
Nov 25 '18 at 15:30
why to close my question? I'm just asking for some help... I tried by myself all possible solutions and now I'm with no option to test and try (no chance to solve this) ... I need little help from other people cause I don't see what I am doing wrong? :(((
– Cristian Babarusi
Nov 25 '18 at 16:02
We expect to see an input which reproduces your issue, the expected output, why this output is expected, and the actual output. You have provided none of these things.
– Joe C
Nov 25 '18 at 16:07
i provided in question pictures with input ... but i will put another picture with some of custom inputs and outputs of my code...
– Cristian Babarusi
Nov 25 '18 at 16:12
|
show 3 more comments
Maybe your code is failing one (or more) of their secret tests?
– Joe C
Nov 25 '18 at 15:29
I am voting to close this question as off-topic because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Joe C
Nov 25 '18 at 15:30
why to close my question? I'm just asking for some help... I tried by myself all possible solutions and now I'm with no option to test and try (no chance to solve this) ... I need little help from other people cause I don't see what I am doing wrong? :(((
– Cristian Babarusi
Nov 25 '18 at 16:02
We expect to see an input which reproduces your issue, the expected output, why this output is expected, and the actual output. You have provided none of these things.
– Joe C
Nov 25 '18 at 16:07
i provided in question pictures with input ... but i will put another picture with some of custom inputs and outputs of my code...
– Cristian Babarusi
Nov 25 '18 at 16:12
Maybe your code is failing one (or more) of their secret tests?
– Joe C
Nov 25 '18 at 15:29
Maybe your code is failing one (or more) of their secret tests?
– Joe C
Nov 25 '18 at 15:29
I am voting to close this question as off-topic because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Joe C
Nov 25 '18 at 15:30
I am voting to close this question as off-topic because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Joe C
Nov 25 '18 at 15:30
why to close my question? I'm just asking for some help... I tried by myself all possible solutions and now I'm with no option to test and try (no chance to solve this) ... I need little help from other people cause I don't see what I am doing wrong? :(((
– Cristian Babarusi
Nov 25 '18 at 16:02
why to close my question? I'm just asking for some help... I tried by myself all possible solutions and now I'm with no option to test and try (no chance to solve this) ... I need little help from other people cause I don't see what I am doing wrong? :(((
– Cristian Babarusi
Nov 25 '18 at 16:02
We expect to see an input which reproduces your issue, the expected output, why this output is expected, and the actual output. You have provided none of these things.
– Joe C
Nov 25 '18 at 16:07
We expect to see an input which reproduces your issue, the expected output, why this output is expected, and the actual output. You have provided none of these things.
– Joe C
Nov 25 '18 at 16:07
i provided in question pictures with input ... but i will put another picture with some of custom inputs and outputs of my code...
– Cristian Babarusi
Nov 25 '18 at 16:12
i provided in question pictures with input ... but i will put another picture with some of custom inputs and outputs of my code...
– Cristian Babarusi
Nov 25 '18 at 16:12
|
show 3 more comments
1 Answer
1
active
oldest
votes
Your code contains a small bug due to the following if-else statements:
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
You forgot the condition when sum equals multiply. In that case, your answer variable remains unchanged in that iteration and you will get a wrong answer finally.
Now sum == multiply can occur for the test case of "22" --> your code gives answer as 2, while the answer should be 4.
This might be one of the test cases failing for your code.
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
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%2f53468839%2fjava-max-value-from-a-string-that-contains-only-digits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code contains a small bug due to the following if-else statements:
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
You forgot the condition when sum equals multiply. In that case, your answer variable remains unchanged in that iteration and you will get a wrong answer finally.
Now sum == multiply can occur for the test case of "22" --> your code gives answer as 2, while the answer should be 4.
This might be one of the test cases failing for your code.
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
add a comment |
Your code contains a small bug due to the following if-else statements:
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
You forgot the condition when sum equals multiply. In that case, your answer variable remains unchanged in that iteration and you will get a wrong answer finally.
Now sum == multiply can occur for the test case of "22" --> your code gives answer as 2, while the answer should be 4.
This might be one of the test cases failing for your code.
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
add a comment |
Your code contains a small bug due to the following if-else statements:
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
You forgot the condition when sum equals multiply. In that case, your answer variable remains unchanged in that iteration and you will get a wrong answer finally.
Now sum == multiply can occur for the test case of "22" --> your code gives answer as 2, while the answer should be 4.
This might be one of the test cases failing for your code.
Your code contains a small bug due to the following if-else statements:
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
You forgot the condition when sum equals multiply. In that case, your answer variable remains unchanged in that iteration and you will get a wrong answer finally.
Now sum == multiply can occur for the test case of "22" --> your code gives answer as 2, while the answer should be 4.
This might be one of the test cases failing for your code.
answered Nov 25 '18 at 15:41
mettleapmettleap
1,065316
1,065316
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
add a comment |
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
thank you... good point, but i tryed like this... and also say to me wrong answer :( what other test cases could be the hidden? If I understand well...could me problem only here on test cases?
– Cristian Babarusi
Nov 25 '18 at 15:53
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
//Your Code here long answer = 0; for (int i = 0; i < str.length(); i++) { long multiply = (str.charAt(i) - 48) * answer; long sum = (str.charAt(i) - 48) + answer; if (multiply > sum) answer = multiply; else if (sum > multiply) answer = sum; else if (multiply == sum) answer = multiply; } System.out.println(answer);
– Cristian Babarusi
Nov 25 '18 at 15:54
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.
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%2f53468839%2fjava-max-value-from-a-string-that-contains-only-digits%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
Maybe your code is failing one (or more) of their secret tests?
– Joe C
Nov 25 '18 at 15:29
I am voting to close this question as off-topic because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Joe C
Nov 25 '18 at 15:30
why to close my question? I'm just asking for some help... I tried by myself all possible solutions and now I'm with no option to test and try (no chance to solve this) ... I need little help from other people cause I don't see what I am doing wrong? :(((
– Cristian Babarusi
Nov 25 '18 at 16:02
We expect to see an input which reproduces your issue, the expected output, why this output is expected, and the actual output. You have provided none of these things.
– Joe C
Nov 25 '18 at 16:07
i provided in question pictures with input ... but i will put another picture with some of custom inputs and outputs of my code...
– Cristian Babarusi
Nov 25 '18 at 16:12