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?
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
add a comment |
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
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
add a comment |
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
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
java android arrays
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
Inside onClick
use a for each
loop:
totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;
int total = SS.stream().filter(s -> s.isChecked).count();
– David Zimmerman
Mar 21 at 20:57
@DavidZimmerman array doesn't have stream() method, count() returnslong
=)
– 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
add a comment |
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!
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
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%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
Inside onClick
use a for each
loop:
totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;
int total = SS.stream().filter(s -> s.isChecked).count();
– David Zimmerman
Mar 21 at 20:57
@DavidZimmerman array doesn't have stream() method, count() returnslong
=)
– 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
add a comment |
Inside onClick
use a for each
loop:
totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;
int total = SS.stream().filter(s -> s.isChecked).count();
– David Zimmerman
Mar 21 at 20:57
@DavidZimmerman array doesn't have stream() method, count() returnslong
=)
– 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
add a comment |
Inside onClick
use a for each
loop:
totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;
Inside onClick
use a for each
loop:
totalactive = 0;
for (Switch s : SS)
if (s.isChecked()) totalactive++;
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() returnslong
=)
– 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
add a comment |
int total = SS.stream().filter(s -> s.isChecked).count();
– David Zimmerman
Mar 21 at 20:57
@DavidZimmerman array doesn't have stream() method, count() returnslong
=)
– 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
add a comment |
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!
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
add a comment |
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!
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
add a comment |
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!
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!
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
add a comment |
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
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%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
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
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