lisak lisak - 1 year ago 75
Java Question

Greater than and less than in one statement

I was wondering, do you have a neat way of doing this ?

if(orderBean.getFiles().size() > 0 && orderBean.getFiles().size() < 5)

without declaring a variable that is not needed anywhere else ?

int filesCount = orderBean.getFiles().size();
if(filesCount > 0 && filesCount < 5) {

I mean, in for loop we are "declaring conditions" for the actual iteration, one can declare a variable and then specify the conditions. Here one can't do it, and neither can do something like

if(5 > orderBean.getFiles().size() > 0)

Answer Source

Simple utility method:

public static boolean isBetween(int value, int min, int max)
  return((value > min) && (value < max));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download