Android: Count number of cases in switch statement, know when last case












0














I am busy working on an app with multiple switch statement that each have a different number of cases.



switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something


I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.



How do I know when the count is at the last case? What is the code for that?



I appreciate your time.










share|improve this question


















  • 2




    What do you mean with "last case"?
    – tm13
    Nov 23 '18 at 5:05






  • 1




    Can't you just add your action on the last case?
    – josh_gom3z
    Nov 23 '18 at 5:05










  • @tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
    – Stark
    Nov 23 '18 at 5:12










  • @josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
    – Stark
    Nov 23 '18 at 5:15






  • 1




    @Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
    – tm13
    Nov 23 '18 at 5:31


















0














I am busy working on an app with multiple switch statement that each have a different number of cases.



switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something


I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.



How do I know when the count is at the last case? What is the code for that?



I appreciate your time.










share|improve this question


















  • 2




    What do you mean with "last case"?
    – tm13
    Nov 23 '18 at 5:05






  • 1




    Can't you just add your action on the last case?
    – josh_gom3z
    Nov 23 '18 at 5:05










  • @tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
    – Stark
    Nov 23 '18 at 5:12










  • @josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
    – Stark
    Nov 23 '18 at 5:15






  • 1




    @Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
    – tm13
    Nov 23 '18 at 5:31
















0












0








0







I am busy working on an app with multiple switch statement that each have a different number of cases.



switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something


I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.



How do I know when the count is at the last case? What is the code for that?



I appreciate your time.










share|improve this question













I am busy working on an app with multiple switch statement that each have a different number of cases.



switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something


I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.



How do I know when the count is at the last case? What is the code for that?



I appreciate your time.







android switch-statement counting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 5:01









Stark

409




409








  • 2




    What do you mean with "last case"?
    – tm13
    Nov 23 '18 at 5:05






  • 1




    Can't you just add your action on the last case?
    – josh_gom3z
    Nov 23 '18 at 5:05










  • @tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
    – Stark
    Nov 23 '18 at 5:12










  • @josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
    – Stark
    Nov 23 '18 at 5:15






  • 1




    @Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
    – tm13
    Nov 23 '18 at 5:31
















  • 2




    What do you mean with "last case"?
    – tm13
    Nov 23 '18 at 5:05






  • 1




    Can't you just add your action on the last case?
    – josh_gom3z
    Nov 23 '18 at 5:05










  • @tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
    – Stark
    Nov 23 '18 at 5:12










  • @josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
    – Stark
    Nov 23 '18 at 5:15






  • 1




    @Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
    – tm13
    Nov 23 '18 at 5:31










2




2




What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05




What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05




1




1




Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05




Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05












@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12




@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12












@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15




@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15




1




1




@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31






@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31














2 Answers
2






active

oldest

votes


















1














You can use default case for your problem.



switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}


You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.






share|improve this answer





















  • Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
    – Stark
    Nov 23 '18 at 5:48



















1














I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch



.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.


[EDIT]



You can restart the variable in the last case of your switch.






share|improve this answer























  • Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
    – Stark
    Nov 23 '18 at 5:47






  • 1




    @Stark, see the edit please.
    – Mijael Viricochea
    Nov 23 '18 at 23:12











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%2f53440878%2fandroid-count-number-of-cases-in-switch-statement-know-when-last-case%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














You can use default case for your problem.



switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}


You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.






share|improve this answer





















  • Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
    – Stark
    Nov 23 '18 at 5:48
















1














You can use default case for your problem.



switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}


You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.






share|improve this answer





















  • Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
    – Stark
    Nov 23 '18 at 5:48














1












1








1






You can use default case for your problem.



switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}


You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.






share|improve this answer












You can use default case for your problem.



switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}


You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 5:31









Rahul Sharma

2,1791418




2,1791418












  • Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
    – Stark
    Nov 23 '18 at 5:48


















  • Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
    – Stark
    Nov 23 '18 at 5:48
















Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48




Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48













1














I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch



.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.


[EDIT]



You can restart the variable in the last case of your switch.






share|improve this answer























  • Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
    – Stark
    Nov 23 '18 at 5:47






  • 1




    @Stark, see the edit please.
    – Mijael Viricochea
    Nov 23 '18 at 23:12
















1














I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch



.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.


[EDIT]



You can restart the variable in the last case of your switch.






share|improve this answer























  • Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
    – Stark
    Nov 23 '18 at 5:47






  • 1




    @Stark, see the edit please.
    – Mijael Viricochea
    Nov 23 '18 at 23:12














1












1








1






I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch



.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.


[EDIT]



You can restart the variable in the last case of your switch.






share|improve this answer














I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch



.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.


[EDIT]



You can restart the variable in the last case of your switch.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 23:10

























answered Nov 23 '18 at 5:26









Mijael Viricochea

5317




5317












  • Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
    – Stark
    Nov 23 '18 at 5:47






  • 1




    @Stark, see the edit please.
    – Mijael Viricochea
    Nov 23 '18 at 23:12


















  • Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
    – Stark
    Nov 23 '18 at 5:47






  • 1




    @Stark, see the edit please.
    – Mijael Viricochea
    Nov 23 '18 at 23:12
















Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47




Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47




1




1




@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12




@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53440878%2fandroid-count-number-of-cases-in-switch-statement-know-when-last-case%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo