Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Switch statements in Java

  • 11-11-2016 4:51pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭


    Can you only work with one variable when working with switch statements?


Comments

  • Closed Accounts Posts: 965 ✭✭✭Thelomen Toblackai


    A switch will only take one variable as far as I'm aware. Depending on what exactly it is you need to do though there may be some work arounds.

    As in if you want to check multiple strings at the same time in each case you could combine them with a separator (e.g. switch(variable1 + "|" + variable2)) and then check that in the case (e.g. case "text1|text2")

    What exactly do you need to do ?


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    One variable is switch statement, if there is any other variables or comparisons, just use an if/else.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭Sephiroth_dude


    A switch will only take one variable as far as I'm aware. Depending on what exactly it is you need to do though there may be some work arounds.

    As in if you want to check multiple strings at the same time in each case you could combine them with a separator (e.g. switch(variable1 + "|" + variable2)) and then check that in the case (e.g. case "text1|text2")

    What exactly do you need to do ?
    Senna wrote: »
    One variable is switch statement, if there is any other variables or comparisons, just use an if/else.

    Hmmmm was thinking that alright.

    Basically The program I'm trying too write will do the following :

    When the program starts a menu which requires user input will appear, In the menu you have three options

    1) Press 1 for Water charges calculator

    2) Press 2 for Childbenefit calculator

    3) press 0 to exit the program

    If option one is chosen a second menu will appear with the following options

    1) press one metered water

    2) press 2 for unmetered

    3) press 0 to exit program

    I thought switch might work better than if/else statements but then I watched a tutorial video on youtube and realised he only had one int variable.


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    For that a switch is perfect, if there is sub menus, you might have a switch inside a switch or a switch option take you to another switch.
    For menus, 99% of the time a switch works best.


  • Closed Accounts Posts: 965 ✭✭✭Thelomen Toblackai


    Hmmmm was thinking that alright.

    Basically The program I'm trying too write will do the following :

    When the program starts a menu which requires user input will appear, In the menu you have three options

    1) Press 1 for Water charges calculator

    2) Press 2 for Childbenefit calculator

    3) press 0 to exit the program

    If option one is chosen a second menu will appear with the following options

    1) press one metered water

    2) press 2 for unmetered

    3) press 0 to exit program

    I thought switch might work better than if/else statements but then I watched a tutorial video on youtube and realised he only had one int variable.

    If it were me I'd have a switch for the input to see what calculator to call. And separate methods for each calculator with their own switches for their own selections if they need them.

    Things to keep in mind when writing things like this are:

    1. How easy is it to maintain? Switches make adding new menu options or feedback for invalid selections a lot easier than multiple if\else statements.

    2. Separating the areas of concern in your program. While it might seem good to combine stuff and avoid a certain amount of duplication it's often better to properly separate those areas out that have a distinct function. It makes testing and debugging issues a lot easier.


  • Advertisement
  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭Sephiroth_dude


    Thanks lads, dont really have a good understanding of methods yet, so will do my research, a method is basically a program within a program?


  • Closed Accounts Posts: 965 ✭✭✭Thelomen Toblackai


    Thanks lads, dont really have a good understanding of methods yet, so will do my research, a method is basically a program within a program?

    Kinda yeah. It's like a separate portion of code in the program that you can call whenever you need it. The execution will jump into the method execute the code and then jump back to where it was.

    If you want to keep it a bit simpler then maybe a switch inside the switch as senna said is the way to go.


Advertisement