how do you sum an Array that is declared as a switch in Java?How do I check if an array includes an object in JavaScript?How do I read / convert an InputStream into a String in Java?How to append something to an array?What's the simplest way to print a Java array?How to insert an item into an array at a specific index (JavaScript)?How do you check if a variable is an array in JavaScript?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?

How can I prove that a state of equilibrium is unstable?

What is an equivalently powerful replacement spell for the Yuan-Ti's Suggestion spell?

GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?

What is the most common color to indicate the input-field is disabled?

What's the meaning of "Sollensaussagen"?

Does the Idaho Potato Commission associate potato skins with healthy eating?

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

How to install cross-compiler on Ubuntu 18.04?

In the UK, is it possible to get a referendum by a court decision?

Sums of two squares in arithmetic progressions

Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)

Does int main() need a declaration on C++?

Is it possible to map the firing of neurons in the human brain so as to stimulate artificial memories in someone else?

How badly should I try to prevent a user from XSSing themselves?

Finitely generated matrix groups whose eigenvalues are all algebraic

In Bayesian inference, why are some terms dropped from the posterior predictive?

Can someone clarify Hamming's notion of important problems in relation to modern academia?

Rotate ASCII Art by 45 Degrees

Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?

Can compressed videos be decoded back to their uncompresed original format?

OP Amp not amplifying audio signal

Can I hook these wires up to find the connection to a dead outlet?

Is it possible to create a QR code using text?

How could indestructible materials be used in power generation?



how do you sum an Array that is declared as a switch in Java?


How do I check if an array includes an object in JavaScript?How do I read / convert an InputStream into a String in Java?How to append something to an array?What's the simplest way to print a Java array?How to insert an item into an array at a specific index (JavaScript)?How do you check if a variable is an array in JavaScript?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?













1















I am new to Java and building an Android app using it, I want to be able to determine how many switches are on but not sure how the best way to sum an array of switches. I currently have:



SS = new Switch[8];
SS[0] = getView().findViewById(R.id.switch1);
SS[1] = getView().findViewById(R.id.switch2);
SS[2] = getView().findViewById(R.id.switch3);
SS[3] = getView().findViewById(R.id.switch4);
SS[4] = getView().findViewById(R.id.switch5);
SS[5] = getView().findViewById(R.id.switch6);
SS[6] = getView().findViewById(R.id.switch7);
SS[7] = getView().findViewById(R.id.switch8);

submit = getView().findViewById(R.id.submitButton);

submit.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
totalactive= IntStream.of(SS).sum();


with the array "SS" declared as a class global.



Thank you










share|improve this question






















  • SS is not compatible with IntStream.of() -- how do you tell if a "Switch" is "on" or "off" - is there a method you can call?

    – Not a JD
    Mar 21 at 20:39











  • I was just messing with you buddy, don't mind me ;-)

    – Taslim Oseni
    Mar 21 at 20:59











  • I use an setOnCheckedChangeListener for each switch but want to limit how many they can turn on at one time.

    – Athos
    Mar 22 at 14:33















1















I am new to Java and building an Android app using it, I want to be able to determine how many switches are on but not sure how the best way to sum an array of switches. I currently have:



SS = new Switch[8];
SS[0] = getView().findViewById(R.id.switch1);
SS[1] = getView().findViewById(R.id.switch2);
SS[2] = getView().findViewById(R.id.switch3);
SS[3] = getView().findViewById(R.id.switch4);
SS[4] = getView().findViewById(R.id.switch5);
SS[5] = getView().findViewById(R.id.switch6);
SS[6] = getView().findViewById(R.id.switch7);
SS[7] = getView().findViewById(R.id.switch8);

submit = getView().findViewById(R.id.submitButton);

submit.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
totalactive= IntStream.of(SS).sum();


with the array "SS" declared as a class global.



Thank you










share|improve this question






















  • SS is not compatible with IntStream.of() -- how do you tell if a "Switch" is "on" or "off" - is there a method you can call?

    – Not a JD
    Mar 21 at 20:39











  • I was just messing with you buddy, don't mind me ;-)

    – Taslim Oseni
    Mar 21 at 20:59











  • I use an setOnCheckedChangeListener for each switch but want to limit how many they can turn on at one time.

    – Athos
    Mar 22 at 14:33













1












1








1








I am new to Java and building an Android app using it, I want to be able to determine how many switches are on but not sure how the best way to sum an array of switches. I currently have:



SS = new Switch[8];
SS[0] = getView().findViewById(R.id.switch1);
SS[1] = getView().findViewById(R.id.switch2);
SS[2] = getView().findViewById(R.id.switch3);
SS[3] = getView().findViewById(R.id.switch4);
SS[4] = getView().findViewById(R.id.switch5);
SS[5] = getView().findViewById(R.id.switch6);
SS[6] = getView().findViewById(R.id.switch7);
SS[7] = getView().findViewById(R.id.switch8);

submit = getView().findViewById(R.id.submitButton);

submit.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
totalactive= IntStream.of(SS).sum();


with the array "SS" declared as a class global.



Thank you










share|improve this question














I am new to Java and building an Android app using it, I want to be able to determine how many switches are on but not sure how the best way to sum an array of switches. I currently have:



SS = new Switch[8];
SS[0] = getView().findViewById(R.id.switch1);
SS[1] = getView().findViewById(R.id.switch2);
SS[2] = getView().findViewById(R.id.switch3);
SS[3] = getView().findViewById(R.id.switch4);
SS[4] = getView().findViewById(R.id.switch5);
SS[5] = getView().findViewById(R.id.switch6);
SS[6] = getView().findViewById(R.id.switch7);
SS[7] = getView().findViewById(R.id.switch8);

submit = getView().findViewById(R.id.submitButton);

submit.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
totalactive= IntStream.of(SS).sum();


with the array "SS" declared as a class global.



Thank you







java android arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 20:37









AthosAthos

32




32












  • SS is not compatible with IntStream.of() -- how do you tell if a "Switch" is "on" or "off" - is there a method you can call?

    – Not a JD
    Mar 21 at 20:39











  • I was just messing with you buddy, don't mind me ;-)

    – Taslim Oseni
    Mar 21 at 20:59











  • I use an setOnCheckedChangeListener for each switch but want to limit how many they can turn on at one time.

    – Athos
    Mar 22 at 14:33

















  • SS is not compatible with IntStream.of() -- how do you tell if a "Switch" is "on" or "off" - is there a method you can call?

    – Not a JD
    Mar 21 at 20:39











  • I was just messing with you buddy, don't mind me ;-)

    – Taslim Oseni
    Mar 21 at 20:59











  • I use an setOnCheckedChangeListener for each switch but want to limit how many they can turn on at one time.

    – Athos
    Mar 22 at 14:33
















SS is not compatible with IntStream.of() -- how do you tell if a "Switch" is "on" or "off" - is there a method you can call?

– Not a JD
Mar 21 at 20:39





SS is not compatible with IntStream.of() -- how do you tell if a "Switch" is "on" or "off" - is there a method you can call?

– Not a JD
Mar 21 at 20:39













I was just messing with you buddy, don't mind me ;-)

– Taslim Oseni
Mar 21 at 20:59





I was just messing with you buddy, don't mind me ;-)

– Taslim Oseni
Mar 21 at 20:59













I use an setOnCheckedChangeListener for each switch but want to limit how many they can turn on at one time.

– Athos
Mar 22 at 14:33





I use an setOnCheckedChangeListener for each switch but want to limit how many they can turn on at one time.

– Athos
Mar 22 at 14:33












2 Answers
2






active

oldest

votes


















1














Inside onClick use a for each loop:



totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;






share|improve this answer























  • int total = SS.stream().filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 21 at 20:57











  • @DavidZimmerman array doesn't have stream() method, count() returns long =)

    – star67
    Mar 21 at 20:58











  • long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 22 at 13:52











  • This solution worked great, thank you for the help everyone.

    – Athos
    Mar 22 at 14:39


















1














All the answers so far are great! But then, you could also do it the old school way:



int totalactive = 0;

for(int i = 0; i < SS.length; i++)
if(SS[i].isChecked())
totalactive++;



//Here's your result:
Log.d("sumTotal", Integer.toString(totalactive));


I bet this is very easy to understand and relate to. I hope this helps. Merry coding!






share|improve this answer























  • This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

    – Athos
    Mar 22 at 14:40











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%2f55288874%2fhow-do-you-sum-an-array-that-is-declared-as-a-switch-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Inside onClick use a for each loop:



totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;






share|improve this answer























  • int total = SS.stream().filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 21 at 20:57











  • @DavidZimmerman array doesn't have stream() method, count() returns long =)

    – star67
    Mar 21 at 20:58











  • long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 22 at 13:52











  • This solution worked great, thank you for the help everyone.

    – Athos
    Mar 22 at 14:39















1














Inside onClick use a for each loop:



totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;






share|improve this answer























  • int total = SS.stream().filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 21 at 20:57











  • @DavidZimmerman array doesn't have stream() method, count() returns long =)

    – star67
    Mar 21 at 20:58











  • long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 22 at 13:52











  • This solution worked great, thank you for the help everyone.

    – Athos
    Mar 22 at 14:39













1












1








1







Inside onClick use a for each loop:



totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;






share|improve this answer













Inside onClick use a for each loop:



totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 20:52









forpasforpas

19.3k4829




19.3k4829












  • int total = SS.stream().filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 21 at 20:57











  • @DavidZimmerman array doesn't have stream() method, count() returns long =)

    – star67
    Mar 21 at 20:58











  • long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 22 at 13:52











  • This solution worked great, thank you for the help everyone.

    – Athos
    Mar 22 at 14:39

















  • int total = SS.stream().filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 21 at 20:57











  • @DavidZimmerman array doesn't have stream() method, count() returns long =)

    – star67
    Mar 21 at 20:58











  • long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

    – David Zimmerman
    Mar 22 at 13:52











  • This solution worked great, thank you for the help everyone.

    – Athos
    Mar 22 at 14:39
















int total = SS.stream().filter(s -> s.isChecked).count();

– David Zimmerman
Mar 21 at 20:57





int total = SS.stream().filter(s -> s.isChecked).count();

– David Zimmerman
Mar 21 at 20:57













@DavidZimmerman array doesn't have stream() method, count() returns long =)

– star67
Mar 21 at 20:58





@DavidZimmerman array doesn't have stream() method, count() returns long =)

– star67
Mar 21 at 20:58













long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

– David Zimmerman
Mar 22 at 13:52





long total = Arrays.stream(SS).filter(s -> s.isChecked).count();

– David Zimmerman
Mar 22 at 13:52













This solution worked great, thank you for the help everyone.

– Athos
Mar 22 at 14:39





This solution worked great, thank you for the help everyone.

– Athos
Mar 22 at 14:39













1














All the answers so far are great! But then, you could also do it the old school way:



int totalactive = 0;

for(int i = 0; i < SS.length; i++)
if(SS[i].isChecked())
totalactive++;



//Here's your result:
Log.d("sumTotal", Integer.toString(totalactive));


I bet this is very easy to understand and relate to. I hope this helps. Merry coding!






share|improve this answer























  • This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

    – Athos
    Mar 22 at 14:40















1














All the answers so far are great! But then, you could also do it the old school way:



int totalactive = 0;

for(int i = 0; i < SS.length; i++)
if(SS[i].isChecked())
totalactive++;



//Here's your result:
Log.d("sumTotal", Integer.toString(totalactive));


I bet this is very easy to understand and relate to. I hope this helps. Merry coding!






share|improve this answer























  • This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

    – Athos
    Mar 22 at 14:40













1












1








1







All the answers so far are great! But then, you could also do it the old school way:



int totalactive = 0;

for(int i = 0; i < SS.length; i++)
if(SS[i].isChecked())
totalactive++;



//Here's your result:
Log.d("sumTotal", Integer.toString(totalactive));


I bet this is very easy to understand and relate to. I hope this helps. Merry coding!






share|improve this answer













All the answers so far are great! But then, you could also do it the old school way:



int totalactive = 0;

for(int i = 0; i < SS.length; i++)
if(SS[i].isChecked())
totalactive++;



//Here's your result:
Log.d("sumTotal", Integer.toString(totalactive));


I bet this is very easy to understand and relate to. I hope this helps. Merry coding!







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 21:02









Taslim OseniTaslim Oseni

1




1












  • This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

    – Athos
    Mar 22 at 14:40

















  • This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

    – Athos
    Mar 22 at 14:40
















This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

– Athos
Mar 22 at 14:40





This is very reminiscent of C and what I was about to fall back on but I went with the other simply because it looked cleaner. I am sure this works as well though, thank you for the advice.

– Athos
Mar 22 at 14:40

















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288874%2fhow-do-you-sum-an-array-that-is-declared-as-a-switch-in-java%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript