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

  • SEARCH
  • Home
  • 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 8711669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:56:09+00:00 2026-06-13T04:56:09+00:00

I’m wondering if it’s possible to use switch statement instead of if-else one in

  • 0

I’m wondering if it’s possible to use switch statement instead of if-else one in this case. Variables are taken from JComboBoxes and processed by ActionEvent here:

public void actionPerformed(ActionEvent event) {

    Object source = event.getSource();

     if (source == comboUnit) {

        String unit = comboUnit.getSelectedItem().toString();

            if (unit.equals("Unit 1")) {
            unitValue = Double.parseDouble(tfUnit.getText());
            valMeter = unitValue * defined1;
            labelDesc.setText("Unit 1");
            convert();
        }

        else if (unit.equals("Unit 2")) {
            unitValue = Double.parseDouble(tfUnit.getText());
            valMeter = unitValue * defined2;
            labelDesc.setText("Unit 2");
            convert();
        }
(...)

I was trying to pass pure strings as the values but with no success. Do you have any hints on how it should be done (and if it’s even possible)?

  • 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-06-13T04:56:11+00:00Added an answer on June 13, 2026 at 4:56 am

    Java 7 allows you to switch on Strings.

    Your code needs refactoring though, and the question of whether to use switch or enumerations or maps, is something that comes second, I would say.

    You have too much duplicated code doing essentially the same thing. I am referring to:

    unitValue = Double.parseDouble(tfUnit.getText());
    valMeter = unitValue * defined1;
    labelDesc.setText("Unit 1");
    convert();
    

    Obviously, because you multiply using different factors depending on the unit used, you need a function of unit evaluating to the factor to use. In less mathematical terms, you need something that yields value of either defined1 or defined2 depending on a string supplied. You already have the unit referring to the "name" of your unit in question, you can use it. I call the method returning the factor for factor, taking in a unit name and returning a Number (since it does not follow from your example whether you are multiplying integers or real numbers of some sort). I am also assuming your defined1 and defined2 etc, are variables or literals.

    unitValue = Double.parseDouble(tfUnit.getText());
    valMeter = unitValue * factor(unit);
    labelDesc.setText(unit);
    convert();
    
    Number factor(String unitName) {
         switch(unitName) {
             case "Unit 1": return defined1;
             case "Unit 2": return defined2;
             default: throw new Exception("Unknown unit");
         }
    }
    

    The method itself is where your "to switch or not to switch" problem creeps in. You are free to use a map if you want:

    Map<String, Number> unitNameValueMap = new HashMap<String, Number>();
    
    unitNameValueMap.put("Unit 1", defined1);
    unitNameValueMap.put("Unit 2", defined2);
    
    Number factor(String unitName) {
        Number result = unitNameValueMap.get(unitName);
        if(result == null) throw new Exception("Unknown unit");
        return result;
    }
    

    Or you can use enumerations:

    enum UnitValue {
        UNIT1(defined1), UNIT2(defined2);
        final Number value;
        private UnitValue(Number value) {
            this.value = value;
        }
    }
    
    Number factor(String unitName) {
        return Enum.valueOf(UnitValue.class, "UNIT" + Integer.parseInt(unitName.substring(5)).value;
    }
    

    You can even switch or use a map inside enumerations, which will give you good code readability as well.

    You need to profile your program to see if you need switch-based solution or enum-based one or a map-based one. The way it currently stands and as you can see for yourself, the enum-based solution is a bit messy because of relationship between your unit names, enumeration constants, and their values. If someone can do better with enums, then take their code instead, naturally.

    Usually, shortest code is best, as it is easier to read and most often understand. But be careful with maps – they induce more overhead than other solutions, so I for one prefer to use them where I have fewer maps with many keys each, not the other way around.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.