Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:39:37+00:00 2026-05-29T19:39:37+00:00

I am using a Java switch statement to assign different values to a variable

  • 0

I am using a Java switch statement to assign different values to a variable based on the id of the button that called it.

The problem is that it always evaluates to the detail.

I think the error might be in how I am getting the Id (the R.java bit), but I have done some internet research, and can’t find the ‘proper’ way of doing it.

This is my code (the -99 and 23 are there so it was obvious if it wasn’t working)

public void rootMe(View v){

 /* I got rid of some code here so it was shorter */

    int mult = -99;

    int buttonId = v.getId();

    switch(buttonId)
    {
    case R.id.root2:
        mult = 1/2;
    case R.id.root3:
        mult = 1/3;
    case R.id.root4:
        mult = 1/4;
    case R.id.root5:
        mult = 1/5;
    case R.id.root6:
        mult = 1/6;
    default:
        mult = 23;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:39:38+00:00Added an answer on May 29, 2026 at 7:39 pm

    With a swicth construct, once a case has been reached, the code below that case will be executed — even if there is another case statement.

    If you want the execution to stop (to exit the switch construct), you must use a break statement ; most probably at the end of each one of your case blocks :

    switch(buttonId)
    {
        case R.id.root2:
            mult = 1/2;
            break;
        case R.id.root3:
            mult = 1/3;
            break;
        case R.id.root4:
            mult = 1/4;
            break;
        case R.id.root5:
            mult = 1/5;
            break;
        case R.id.root6:
            mult = 1/6;
            break;
        default:
            mult = 23;
            break;
    }
    

    This way, when the code that corresponds to a case statement is executed, it will no longer execute the code that corresponds to the following cases statements, and exit the switch construct.

    Note that this is not android-specific : it’s just the way the switch statement works in JAVA ; see for exemple : The switch Statement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Java, how can I test that a URL is contactable, and returns a
Using Java (1.6) I want to split an input string that has components of
While using Java's switch case, it excepts only char and int, but I want
I am facing a certain behavior using Amazon EC2 and Java that it's being
For readability reasons I'm trying to avoid using Char based case constructs, using Java
I am currently using a switch statement to handle types of incoming messages of
I have an issue with accessing variables initialized in a switch-statement, and then using
I'm using Java 6. Suppose I have an enum with 6 values, ordered A
Currently I'm using Java jdk 1.6.0_26 for my eclipse based application. As now Java
How to switch from one to another tab using jQuery,java script???

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.