Search code examples
javaoperators

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)

Solution

  • Simple utility method:

    public static boolean isBetween(int value, int min, int max)
    {
      return((value > min) && (value < max));
    }