matthijsW
matthijsW

Reputation: 387

Check if a variable is between two numbers with Java

I have a problem with this code:

if (90 >>= angle =<< 180)

The error explanation is:

The left-hand side of an assignment must be a variable.

I understand what this means but how do I turn the above code into correct code?

Upvotes: 21

Views: 314258

Answers (10)

KayG
KayG

Reputation: 11

if (age >= 90 && age <=180) {
    // statements
}

This checks is a number is between, you can also use a built in function called Range or any other built function that can be of use like range between

Upvotes: 0

Marco Carlo Moriggi
Marco Carlo Moriggi

Reputation: 414

I think that using a static method can be a benefit in terms of readability and if such a static method could be defined as a standard, there could be also improvements in terms of performance: let me say, imagine that in java.lang.Math you have a method isBetween(int toCheck, int from, int to), which is defined as

public static boolean isBetween(int toCheck, int from, int to){
  // eventually validate the range
  return from <= toCheck && toCheck <= to;
}

the first benefit can be that, if you import static Math.isBetween, than tou can write code like

if(isBetween(userInput, 0, 16)){
  //...
}

and the second benefit would be that, hitting this method many times (for boundary checks, for example, in Strings, arrays, or other statically-sized data structures), can lead to an easily C2-compiled method (probably inlined), and eventually if someone points out that the HotSpot compiler generates a branch, due to the fact that there is a conditionated jump, then can write an improvement that generates brachless code, and every application of this method can benefit, just upgrading to a newer runtime.

Another improvement in readability could come from project Valhalla: if ints can have methods, than you could just write if(userInput.isBetween(0, 16))

Upvotes: 0

user2569050
user2569050

Reputation: 455

public static boolean between(int variable, int minValueInclusive, int maxValueInclusive) {
    return variable >= minValueInclusive && variable <= maxValueInclusive;
}

A Java method to determine if an integer is between a range

Upvotes: 5

James Mudd
James Mudd

Reputation: 2373

I think using org.apache.commons.lang3.Range is a nice solution for this. It can also integrate nicely with Guava Preconditions for argument checking. Some example code showing usage is

import org.apache.commons.lang3.Range;

import java.util.List;

class Scratch {
    public static void main(String[] args) {
        // Create a range object
        Range<Double> validRange = Range.between(90.0, 180.0);
        // and some test values
        List<Double> testValues = List.of(65.3, 89.99, 90.0, 90.11, 134.4, 177.7, 180.00, 180.000001, 32321.884);
        // Test them all
        for (Double testValue : testValues) {
            // Check if it's in the range
            final boolean inRange = validRange.contains(testValue);
            System.out.println(String.format("[%f] in range [%b]", testValue, inRange));
        }
    }
}

it prints

[65.300000] in range [false]
[89.990000] in range [false]
[90.000000] in range [true]
[90.110000] in range [true]
[134.400000] in range [true]
[177.700000] in range [true]
[180.000000] in range [true]
[180.000001] in range [false]
[32321.884000] in range [false]

Upvotes: 1

Mchrom
Mchrom

Reputation: 61

//If "x" is between "a" and "b";

.....

int m = (a+b)/2;

if(Math.abs(x-m) <= (Math.abs(a-m)))

{
(operations)
}

......

//have to use floating point conversions if the summ is not even;

Simple example :

//if x is between 10 and 20

if(Math.abs(x-15)<=5)

Upvotes: 5

peter
peter

Reputation: 15109

are you writing java code for android? in that case you should write maybe

if (90 >= angle && angle <= 180) {

updating the code to a nicer style (like some suggested) you would get:

if (angle <= 90 && angle <= 180) {

now you see that the second check is unnecessary or maybe you mixed up < and > signs in the first check and wanted actually to have

if (angle >= 90 && angle <= 180) {

Upvotes: 2

aliteralmind
aliteralmind

Reputation: 20163

Assuming you are programming in Java, this works:

if (90 >= angle  &&  angle <= 180 )  {

(don't you mean 90 is less than angle? If so: 90 <= angle)

Upvotes: 0

AlexWien
AlexWien

Reputation: 28727

I see some errors in your code.
Your probably meant the mathematical term

90 <= angle <= 180, meaning angle in range 90-180.

if (angle >= 90 && angle <= 180) {

// do action
}

Upvotes: 66

David Ehrmann
David Ehrmann

Reputation: 7576

<<= is like +=, but for a left shift. x <<= 1 means x = x << 1. That's why 90 >>= angle doesn't parse. And, like others have said, Java doesn't have an elegant syntax for checking if a number is an an interval, so you have to do it the long way. It also can't do if (x == 0 || 1), and you're stuck writing it out the long way.

Upvotes: 4

Related Questions