0

If this question was repeated, I'm sorry but I couldn't find the answer. My question is how to code for "If any of these variables equals this" and change it to a new value corresponding to that variable. For instance, I'm trying to do HTML conversions:

        int hex1 = (int)(Math.floor((work[j][k]) / 1048576)) % 16;
        int hex2 = (int)(Math.floor((work[j][k]) / 65536)) % 16;
        int hex3 = (int)(Math.floor((work[j][k]) / 4096)) % 16;
        int hex4 = (int)(Math.floor((work[j][k]) / 256)) % 16;
        int hex5 = (int)(Math.floor((work[j][k]) / 16)) % 16;
        int hex6 = (int)(work[j][k]) % 16;

Above would be my list of multiple variables. So pseudocode would be "if any of the above variables equals this". So if either hex1,hex2,hex3,hex4,hex5,hex6 equals, say 10. Then the corresponding variable would do something. For instance:

         String html = "";
           if (hex1==10){
              html += "A";
           }
           else if (hex1==11){
              html += "B";
           }
           else if (hex1 >= 0 && hex1 <=9){
              html += hex1;
           }
        html = "#" + html;

Is there a way to do this above code, without having to copy/paste the code 6 different times with a different variable each time (ie, hex1 would be hex2, hex3,...)?

user2908614
  • 3
  • 1
  • 3
  • It looks like you know what an array is, since you're using one in your code. So maybe you could use that knowledge to answer your last question? – ajb Oct 29 '13 at 19:36
  • of course it is, take a look [here](http://stackoverflow.com/a/12005557/1029621) – Eugen Halca Oct 29 '13 at 19:37
  • I don't see that the code and pseudocode match here... – clwhisk Oct 29 '13 at 19:41
  • Suppose you have an universal problem solver, then you map each problem with the cause to some location. Then you use the cause and location to alternate behavior, like polymorphic switch. –  Oct 29 '13 at 19:42
  • So, like `var = applyMath(var, )`, where applyMath is just a switch statement that returns the modified var? (String is immutable, so you have to update the variable on function return) – Tezra Apr 13 '18 at 17:22

3 Answers3

0

You can create a function and call that with different values of variables:

void doSomething(int in){
 // put your logic here
}

doSomething(hex1);
doSomething(hex2);

and so on...

csn
  • 376
  • 7
  • 18
0

Yes, you are looking for Maps. Use a Map<Integer, String> in your case. Then you could do:

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(10, "A");
map.put(11, "B");
String str = map.get(hex5);
if (str != null)
{
    html += str;
}

Please, use arrays, instead of hex1, hex2, hex3, hex4, etc... Create a simple:

int[] hex = new int[6];

Next you could optimize those lines to something like this:

int hex5 = (int)(Math.floor((work[j][k]) / 16)) % 16;

would be:

int hex5 = (work[j][k] >> 4) & 0xF;

which is much much faster, assuming work[j][k] is an integer.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

Try something like this:

int[] hex = new int[6];

//code to fill in hex digits

for(int i = 0; i < hex.length;i++){
    if(hex[i]==10){
        //do stuff
    }else if(hex[i]==11){
        //do stuff
    }
   //other conditions

}

EDIT: Or you could use a list to contain arbitrary numbers of hex digits as follows:

List<Integer> hexList = new ArrayList<Integer>();

//code to add hex digits to list

if(hexList.contains(10)){
//do stuff
}else if(hexList.contains(11)){
//do stuff
}
//other conditions
Davis Broda
  • 4,102
  • 5
  • 23
  • 37