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 6589147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:08:08+00:00 2026-05-25T17:08:08+00:00

I know it sounds terrible but in my java program I have about 100

  • 0

I know it sounds terrible but in my java program I have about 100 if-else statements, all containing 3 variables that have to stay unique to those if-else blocks, 1 is a flag used to know when its the first time ever hitting that if-else block, and the other 2 are both strings and are temp variables holding that data that was used last time it ran through that if-else block, so it can be compared to the data running through this time around, sorry if it sounds sloppy, I hate the idea of having so many if-else blocks, but right now Im more concerned about the variables, because if I make 3 variables for each block that is an extra 300 variables. Any suggestions on something I could implement to reduce the amount of variables, one idea I have was 1 array for all the flags and then a 2d array holding the 2 strings for each if-else block. Thanks Beef.

Edited: to show sample of the first 2 if-else blocks, all the other have the same code inside just with different names for the flag and temp variables ex. ac101Flag, tempAC101Start, tempAC101End

                // AC 101
                if (room.equals("FEB 2009") || room.equals("FEB 2011") ||room.equals("FEB 2013") || room.equals("FEB 2015") || room.equals("FEB 2017") ||
                        room.equals("FEB 2021") || room.equals("FEB 2023") || room.equals("FEB 2025") || room.equals("FEB 2027") || room.equals("FEB 2029")) {
                    instanceNum = 4;
                    devID = 130200;
                    if (ac101Flag == false) {
                        Delete();
                        Insert();
                        ac101Flag = true;
                        tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
                        tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
                    }
                    //Insert();
                    else if (tempAC101Start <= (Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'))) && tempAC101End >= Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'))) {

                    }
                    else
                    {
                        Insert();
                        tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
                        tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
                    }   
                }
                // AC 102
                else if(room.equals("FEB 1130")) {
                    instanceNum = 4;
                    devID = 130400;
                    if (ac102Flag == false) {
                        Delete();
                        Insert();
                        ac102Flag = true;
                        tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
                        tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
                    }
                    //Insert();
                    else if (tempAC101Start <= (Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'))) && tempAC101End >= Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'))) {

                    }
                    else
                    {
                        Insert();
                        tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
                        tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
                    }   
                }
  • 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-25T17:08:09+00:00Added an answer on May 25, 2026 at 5:08 pm

    EDIT: Just to give a more concrete demonstration of the answer, I think you want something like:

    class Foo // Rename this!
    {
        private double start;
        private double end;
        private boolean flag;
    
        public void handleValue(double newStart, double newEnd)
        {
            // Insert code here
        }
    }
    
    private static void insertFoo(Map<String, Foo> map, String... rooms)
    {
        Foo foo = new Foo();
        for (String room : rooms)
        {
            map.put(room, foo);
        }
    }
    
    ...
    HashMap<String, Foo> map = new HashMap<String, Foo>();
    insertFoo(map, "FEB 2009", "FEB 2011", ...);
    insertFoo(map, "FEB 1130");
    

    Then in your loop, just use:

    Foo foo = map.get(room);
    foo.update(start, end); // Parsed from the current finalStart/finalEnd
    

    It’s hard to say for sure without seeing your code, but it sounds like you should encapsulate all of this in a data structure:

    • The three variables (state and falg)
    • “Something” to represent the condition of the if part. The exact nature of this will depend on what you’ve got

    You can then run through a list of these ConditionBlock objects (or whatever you choose to call them) and check whether the block “matches” in the current context, updating it where appropriate.

    If you can give us a small example of the original code, we can probably refactor it for you fairly easily.

    EDIT: As I apparently haven’t been clear about introducing the local variables for the sake of parsing in one place, I’d do something like this (as one first step):

    // TODO: Don't use double here - it's completely inappropriate. Use
    // BigDecimal if you absolutely must, but preferrably use a time-related
    // type, e.g. something from Joda Time (http://joda-time.sf.net)
    double parsedStart = Double.parseDouble(finalStart.substring(0, 5)
                                                      .replace(':', '.'));
    
    // TODO: Put all of these constants in a HashSet<String> and use contains
    if (room.equals("FEB 2009") || 
        room.equals("FEB 2011") ||
        room.equals("FEB 2013") || 
        room.equals("FEB 2015") || 
        room.equals("FEB 2017") ||
        room.equals("FEB 2021") || 
        room.equals("FEB 2023") || 
        room.equals("FEB 2025") || 
        room.equals("FEB 2027") || 
        room.equals("FEB 2029")) {
    
        instanceNum = 4;
        devID = 130200;
        // TODO: Change to if (!ac101Flag)
        if (ac101Flag == false) {
            // TODO: Rename these methods to follow Java naming conventions
            Delete();
            Insert();
            ac101Flag = true;
            tempAC101Start = parsedStart;
            tempAC101End = parsedEnd;
        }
        //Insert();
        else if (tempAC101Start <= parsedStart && tempAC101End >= parsedEnd) {
    
        }
        else
        {
            Insert();
            tempAC101Start = parsedStart;
            tempAC101End = parsedEnd;
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know it sounds really stupid, but I have a really easy application that
I know this sounds very dumb, but we have a client that sends out
I know this sounds knit picky but I have several custom controls that have
I know the question sounds silly, but consider this: I have an array of
I know, I know, its sounds silly, but it seems that there are no
I know this sounds like something I can google, but the truth is that
I know it sounds weird but is there a way that an app (facebook
I know that sounds really weird, but I am fairly new to C# and
I know this sounds bizarre, but it is true. I have a simply WPF
I know it sounds so dumb but people were asking me about it and

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.