Getting a wrong output using arraylistsDoes a finally block always get executed in Java?Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to get an enum value from a string value in Java?How to get the last value of an ArrayListInitialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] array
Was the dragon prowess intentionally downplayed in S08E04?
FIFO data structure in pure C
Square spiral in Mathematica
Why would you put your input amplifier in front of your filtering for and ECG signal?
Canadian citizen who is presently in litigation with a US-based company
Why are lawsuits between the President and Congress not automatically sent to the Supreme Court
How to know the path of a particular software?
Pedaling at different gear ratios on flat terrain: what's the point?
Why is vowel phonology represented in a trapezoid instead of a square?
Cuban Primes
When did Britain learn about American independence?
Why does string strummed with finger sound different from the one strummed with pick?
How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5
SHAKE-128/256 or SHA3-256/512
Bash grep result from command whole line
Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?
Why is it correct to use ~た in this sentence, even though we're talking about next week?
What color to choose as "danger" if the main color of my app is red
Deleting the same lines from a list
How was the blinking terminal cursor invented?
Solenoid fastest possible release - for how long should reversed polarity be applied?
Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?
Why are there five extra turns in tournament Magic?
Why do galaxies collide?
Getting a wrong output using arraylists
Does a finally block always get executed in Java?Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to get an enum value from a string value in Java?How to get the last value of an ArrayListInitialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.
Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
List<String> digits = new ArrayList<String>();
String num;
int sum = 0, multi;
for (int i=a; i<=b; i++)
num = String.valueOf(i);
digits.add(num);
for (int j=0; j<digits.size(); j++)
multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
digits.clear();
return eureka;
With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].
I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.
java string arraylist
New contributor
add a comment |
The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.
Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
List<String> digits = new ArrayList<String>();
String num;
int sum = 0, multi;
for (int i=a; i<=b; i++)
num = String.valueOf(i);
digits.add(num);
for (int j=0; j<digits.size(); j++)
multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
digits.clear();
return eureka;
With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].
I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.
java string arraylist
New contributor
add a comment |
The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.
Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
List<String> digits = new ArrayList<String>();
String num;
int sum = 0, multi;
for (int i=a; i<=b; i++)
num = String.valueOf(i);
digits.add(num);
for (int j=0; j<digits.size(); j++)
multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
digits.clear();
return eureka;
With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].
I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.
java string arraylist
New contributor
The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.
Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
List<String> digits = new ArrayList<String>();
String num;
int sum = 0, multi;
for (int i=a; i<=b; i++)
num = String.valueOf(i);
digits.add(num);
for (int j=0; j<digits.size(); j++)
multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
digits.clear();
return eureka;
With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].
I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.
java string arraylist
java string arraylist
New contributor
New contributor
edited May 11 at 18:56
Nicholas K
8,99571839
8,99571839
New contributor
asked May 11 at 12:48
PrometeusHPrometeusH
584
584
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use the following:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
for (int j = 0; j < num.length(); j++)
multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
sum += multi;
if (sum == i)
eureka.add(i);
sum = 0;
return eureka;
Explanation:
- You were not checking the second digit of the number.
- Loop over each character of the String
num
. - There is no need of the
digits
arraylist, you can just use the numeric value of the char.
add a comment |
The problem were those lines :
num = String.valueOf(i);
digits.add(num);
You did not split your number into digits. You were just putting your whole numbers into digits
list. Look at this code :
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
List<String> digits;
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
digits = Arrays.asList(num.split(""));
for (int j = 0; j < digits.size(); j++)
multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
I simply split your string number into digits using Arrays.asList(num.split(""))
. It outputs for a=1
, b=100
the list :
1
2
3
4
5
6
7
8
9
89
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
add a comment |
Use char[]
to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):
...
char[] digits;
...
digits = String.valueOf(i).toCharArray();
Then if you subtract '0'
from each char
digit you automatically get the actual int
value of the digit without having to invoke the Integer.parseInt
method on a String, or any other parsing method:
(int)Math.pow(digits[j] - '0', j + 1);
The full code would look like this:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
int sum = 0;
char[] digits;
for (int i = a; i <= b; i++)
digits = String.valueOf(i).toCharArray();
for (int j = 0; j < num.length(); j++)
sum += (int)Math.pow(digits[j] - '0', j + 1);
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
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
);
);
PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.
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%2f56090501%2fgetting-a-wrong-output-using-arraylists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the following:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
for (int j = 0; j < num.length(); j++)
multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
sum += multi;
if (sum == i)
eureka.add(i);
sum = 0;
return eureka;
Explanation:
- You were not checking the second digit of the number.
- Loop over each character of the String
num
. - There is no need of the
digits
arraylist, you can just use the numeric value of the char.
add a comment |
You can use the following:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
for (int j = 0; j < num.length(); j++)
multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
sum += multi;
if (sum == i)
eureka.add(i);
sum = 0;
return eureka;
Explanation:
- You were not checking the second digit of the number.
- Loop over each character of the String
num
. - There is no need of the
digits
arraylist, you can just use the numeric value of the char.
add a comment |
You can use the following:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
for (int j = 0; j < num.length(); j++)
multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
sum += multi;
if (sum == i)
eureka.add(i);
sum = 0;
return eureka;
Explanation:
- You were not checking the second digit of the number.
- Loop over each character of the String
num
. - There is no need of the
digits
arraylist, you can just use the numeric value of the char.
You can use the following:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>(0);
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
for (int j = 0; j < num.length(); j++)
multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
sum += multi;
if (sum == i)
eureka.add(i);
sum = 0;
return eureka;
Explanation:
- You were not checking the second digit of the number.
- Loop over each character of the String
num
. - There is no need of the
digits
arraylist, you can just use the numeric value of the char.
edited May 11 at 13:12
answered May 11 at 13:03
Nicholas KNicholas K
8,99571839
8,99571839
add a comment |
add a comment |
The problem were those lines :
num = String.valueOf(i);
digits.add(num);
You did not split your number into digits. You were just putting your whole numbers into digits
list. Look at this code :
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
List<String> digits;
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
digits = Arrays.asList(num.split(""));
for (int j = 0; j < digits.size(); j++)
multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
I simply split your string number into digits using Arrays.asList(num.split(""))
. It outputs for a=1
, b=100
the list :
1
2
3
4
5
6
7
8
9
89
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
add a comment |
The problem were those lines :
num = String.valueOf(i);
digits.add(num);
You did not split your number into digits. You were just putting your whole numbers into digits
list. Look at this code :
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
List<String> digits;
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
digits = Arrays.asList(num.split(""));
for (int j = 0; j < digits.size(); j++)
multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
I simply split your string number into digits using Arrays.asList(num.split(""))
. It outputs for a=1
, b=100
the list :
1
2
3
4
5
6
7
8
9
89
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
add a comment |
The problem were those lines :
num = String.valueOf(i);
digits.add(num);
You did not split your number into digits. You were just putting your whole numbers into digits
list. Look at this code :
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
List<String> digits;
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
digits = Arrays.asList(num.split(""));
for (int j = 0; j < digits.size(); j++)
multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
I simply split your string number into digits using Arrays.asList(num.split(""))
. It outputs for a=1
, b=100
the list :
1
2
3
4
5
6
7
8
9
89
The problem were those lines :
num = String.valueOf(i);
digits.add(num);
You did not split your number into digits. You were just putting your whole numbers into digits
list. Look at this code :
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
List<String> digits;
String num;
int sum = 0, multi;
for (int i = a; i <= b; i++)
num = String.valueOf(i);
digits = Arrays.asList(num.split(""));
for (int j = 0; j < digits.size(); j++)
multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
sum += multi;
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
I simply split your string number into digits using Arrays.asList(num.split(""))
. It outputs for a=1
, b=100
the list :
1
2
3
4
5
6
7
8
9
89
edited May 11 at 13:06
answered May 11 at 13:00
michalkmichalk
1,141417
1,141417
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
add a comment |
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.
– PrometeusH
May 11 at 13:06
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
There are more optimizations that could be made but I tried not to modify your code that much :)
– michalk
May 11 at 13:07
add a comment |
Use char[]
to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):
...
char[] digits;
...
digits = String.valueOf(i).toCharArray();
Then if you subtract '0'
from each char
digit you automatically get the actual int
value of the digit without having to invoke the Integer.parseInt
method on a String, or any other parsing method:
(int)Math.pow(digits[j] - '0', j + 1);
The full code would look like this:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
int sum = 0;
char[] digits;
for (int i = a; i <= b; i++)
digits = String.valueOf(i).toCharArray();
for (int j = 0; j < num.length(); j++)
sum += (int)Math.pow(digits[j] - '0', j + 1);
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
add a comment |
Use char[]
to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):
...
char[] digits;
...
digits = String.valueOf(i).toCharArray();
Then if you subtract '0'
from each char
digit you automatically get the actual int
value of the digit without having to invoke the Integer.parseInt
method on a String, or any other parsing method:
(int)Math.pow(digits[j] - '0', j + 1);
The full code would look like this:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
int sum = 0;
char[] digits;
for (int i = a; i <= b; i++)
digits = String.valueOf(i).toCharArray();
for (int j = 0; j < num.length(); j++)
sum += (int)Math.pow(digits[j] - '0', j + 1);
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
add a comment |
Use char[]
to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):
...
char[] digits;
...
digits = String.valueOf(i).toCharArray();
Then if you subtract '0'
from each char
digit you automatically get the actual int
value of the digit without having to invoke the Integer.parseInt
method on a String, or any other parsing method:
(int)Math.pow(digits[j] - '0', j + 1);
The full code would look like this:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
int sum = 0;
char[] digits;
for (int i = a; i <= b; i++)
digits = String.valueOf(i).toCharArray();
for (int j = 0; j < num.length(); j++)
sum += (int)Math.pow(digits[j] - '0', j + 1);
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
Use char[]
to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):
...
char[] digits;
...
digits = String.valueOf(i).toCharArray();
Then if you subtract '0'
from each char
digit you automatically get the actual int
value of the digit without having to invoke the Integer.parseInt
method on a String, or any other parsing method:
(int)Math.pow(digits[j] - '0', j + 1);
The full code would look like this:
static List<Integer> sumDigPow(int a, int b)
List<Integer> eureka = new ArrayList<Integer>();
int sum = 0;
char[] digits;
for (int i = a; i <= b; i++)
digits = String.valueOf(i).toCharArray();
for (int j = 0; j < num.length(); j++)
sum += (int)Math.pow(digits[j] - '0', j + 1);
if (sum == i) eureka.add(i);
sum = 0;
return eureka;
edited May 11 at 16:41
answered May 11 at 13:04
Marco R.Marco R.
398211
398211
add a comment |
add a comment |
PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.
PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.
PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.
PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.
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%2f56090501%2fgetting-a-wrong-output-using-arraylists%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