Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to either remove vars from an array or select what vars can be printed via a print statement  RSS feed

 
Tanner odell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the crap paste job. question is how to output my array i made without all of the 0s it includes, the array is made in the "testPerfect" method the "printFactors" method is what produces the print statement. I have to use a normal array and it has to be of size 100.



[Edit: fixed code tags]
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where exactly are you stuck? Where can we help you? What do you expect your code to do, what does it instead?
 
Tanner odell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...im stuck with how to output the array i have made so that it doesnt include the 0s that are in it, is there a way to go about doing via print statement formatting or am i going to have to take the array ive made and do something else with it. as of now its of size 100 which it has to be there are not 100 factors to say the number 6 there are only 4 so the array is putting 0s into every other spot. so thats 96 zeros, when i print the array it has all of those 0s how can i get rid of those? i know i can do this with a arrayList but i cannot use one of those for this.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will there ever be a case where there is a 0 in the array that you put there? Is it only zeroes that you don't want to show?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanner odell wrote:...im stuck with how to output the array i have made so that it doesnt include the 0s that are in it, is there a way to go about doing via print statement formatting or am i going to have to take the array ive made and do something else with it. as of now its of size 100 which it has to be there are not 100 factors to say the number 6 there are only 4 so the array is putting 0s into every other spot. so thats 96 zeros, when i print the array it has all of those 0s how can i get rid of those? i know i can do this with a arrayList but i cannot use one of those for this.

Right, well:
1. Nothing is "putting" 0's in your array (except perhaps the JVM). It is the "default" value for an int, so when you set up an array of int's, they will ALL be set to 0 initially.
2. No, there is no formatting that I know of that will "ignore 0's". It's your specific requirement, so you need to do it yourself.
3. As I see it, you have two options:
(a) Loop through your array, and only print out elements that are not 0.
(b) Copy the non-zero elements of your array to another array of the correct size. That task might be made easier if you keep track of how many 'factors' you fill in.

It should be added that all of the above assume that 0 cannot possibly be a "valid" value, which for factors is probably true.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!