Results 1 to 6 of 6
  1. #1
    jingly99 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Question Replacing Else If

    I am looking for a way to replace ELSE if condition. for example I have:

    if (this.test.equals("A")) {
    this.test= "B";

    this.service = "B2";
    return true;
    }
    else if (this.test.equals("B")) {
    this.carrierServiceLevel = "C";

    this.service = "C2";
    return true;
    }

    Just like above we have like 10 else if.. all of that is in sequence. So if A fails, than B, else C, else D,, etc.

    How can I replace this else if using some different way. I want to store this a, b c, values in DB so in future I can easily change the value in DB than hardcoding in Java..

    Please let me know how can I replace this else if...

    Thanks

  2. #2
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    You can code your static values as enumerations (enums) and use the switch statement if you want ;-)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  3. #3
    jingly99 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    I dont want static values, i want it to be dynamic, I want to store it in db and then remove the else if logic..

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Can you provide some example scenario?

    If I get you right, then you can store in your db a value of the classname and then load it via the classloader. But it all depends. For me a more precise description of your problem would be great.
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  5. #5
    jingly99 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Currently, what we do is,...

    Default value selected is A, then application runs some logic, if it return "FAILURE", we want user to select next value B, again if it returns "FAILURE" , select C,

    Currently we are doing this by

    if(this.test.equals("A"){
    this.test = "B"
    return true;

    }
    else if(this.test.equals("B"){
    this.test = "C"
    return true;
    }

    we do this from A-F... we are using this else if to check what user has selected based on that we assign this.test to next value....

    Now their is a change in values.. all the values will be for example.. instead of A it will AA, B it will be BB..

    so what we decided is to store this value AA, BB , in Database, assing some sequence like AA is 1, BB is 2,, then read this table, and assign this.test to whatever the next value should be , by replacing if else and hardcoded values in that....

    i hope it makes sense...

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Hm, maybe you can write a value class that defines a succesfunction.
    And provide a method to check whether the value is valid.

    Java Code:
    public class Value {
      
           // provide logic for the value, like the real value and additional information
    
          public Value getSuccessor() {
                  Value currentValue = this;
                  // determine the next value given the "this" val (via DB?)
                 ...
          }
    }
    The calling method would look like this
    Java Code:
    // if failure
    if (!doTestValue(test.currentValue)) {
       test.currentValue = currentValue.getSuccessor();
    }
    return true;
    does this help?
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. Replacing char in string help
    By jimmy-lin in forum New To Java
    Replies: 3
    Last Post: 10-12-2009, 06:01 AM
  2. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 12:31 PM
  3. Help Replacing String
    By 7oclock in forum New To Java
    Replies: 5
    Last Post: 02-14-2009, 07:31 AM
  4. replacing characters???
    By manda147 in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 08:19 AM
  5. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •