As suggested above, there is a much more efficient method to achieve this functionality. Rather than creating a new array with range() each time, we can simply test if the variable is within two bounds.
For example:
x = 12;
if(5 <= x && x <= 15){
System.out.println("x in range");
}
Will print “x in range” because 12 is both greater than 5 and less than 15. As described by user above, you could turn this into a function:
boolean inRange(int x, int lower, int upper){
return (lower <= x && x <= upper);
}
This code is simple and efficient, and should do exactly what you require!
I need to make a conversion table that essentially says if variable 1 is in this range and variable 2 is in this range then variable 3 = x. I’ve done this in PHP and it works, however for my current project PHP won’t work. Is there another straight forward way to accomplish this via Java or something? If anyone could just steer me in the right direction that’d be great!
here is a snipet of the working PHP version:
if(in_array($Var1, range(-10,-1))){
if(in_array($Var2, range(0,1049))){
$var3 = "8";
}
if(in_array($Var2, range(1050,1149))){
$var3 = "9";
}
if(in_array($Var2, range(1150,1249))){
$var3 = "9";
}
if(in_array($Var2, range(1250,1349))){
$var3 = "10";
}
if(in_array($Var2, range(1350,1449))){
$var3 = "11";
}
if(in_array($Var2, range(1450,1550))){
$var3 = "12";
}
}
if(in_array($cwr, range(-20,-11))){
if(in_array($Var2, range(0,1049))){
$var3 = "9";
}
if(in_array($Var2, range(1050,1149))){
$var3 = "9";
}
if(in_array($Var2, range(1150,1249))){
$var3 = "10";
}
if(in_array($Var2, range(1250,1349))){
$var3 = "11";
}
if(in_array($Var2, range(1350,1449))){
$var3 = "12";
}
if(in_array($Var2, range(1450,1550))){
$var3 = "13";
}
}
Isn’t this some really inefficient PHP code? You are creating an array of 50 integers, followed by an expensive array lookup, for every simple test of whether a variable falls between the extremes of each range. @user’s straightforward answer in Java should best be implemented in place of your PHP.
I’m voting to close this question because the question asks for code to be translated – it doesn’t include what they’ve tried so far to achieve that, and it doesn’t involve some language feature that’s present in PHP that they’re trying to find an equivalent of in Java.