Question

class CalorieExpenditures {
        String activity
        int lbs90
        int lbs100
        int lbs110
        int lbs120
        int lbs130
        int lbs140
        int lbs150
        int lbs160
        int lbs170
        int lbs180
        int lbs190
        int lbs200
        int lbs220
        int lbs240
        int lbs260
        int lbs280
        int lbs300              
}

From the above POGO (b'se i am using grails). How to find the nearest lbs property from POGO e.g if i pass 282 it will return lbs280 and if i pass 295 it will return lbs300. logic is the difference between two values if the difference is same will return the grater value.

You can suggest java method or grails method to work with.

I need a simple program that finds nearest value.

Thanks in advance.

Was it helpful?

Solution

Below is example to find given number from int array, with nearest lower & upper

public class FindNearestInt
{
    public static void main(String[] args)
    {
        Random random = new Random();
        int array[] = new int[30];
        //Initialise array with rendom values
        for (int i = 0; i < array.length; i++)
        {
            array[i] = random.nextInt(200);
        }

        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

        // Number you want to find in array
        int searchFor = 57;
        //Nearest lower you searching for  
        int nearestLower = 0;
        //Nearest upper you searching for 
        int nearestUpper = Integer.MAX_VALUE;
        int searchForExist = -1;
        for (int i = 0; i < array.length; i++)
        {
            int j = array[i];
            if (j < searchFor)
            {
                if(j > nearestLower){
                    nearestLower = j;
                }
            } else if (j > searchFor){
                if(j < nearestUpper){
                    nearestUpper = j;
                }
            } else {
                nearestLower = -1;
                nearestUpper = -1;
                searchForExist = j;
                break;
            }
        }
        System.out.println("Nearest Lower : " + nearestLower);
            // This will print -1 if number exist in array
        System.out.println("Provided Number Already Exist : " + searchForExist);
        System.out.println("Nearest Upper : " + nearestUpper);
    }
}

OTHER TIPS

A simpler and faster solution is to use a formula

public static int nearest(int num) {
    return num < 90 ? 90 : 
           num < 200 ? Math.round(num/10.0) * 10 :
           num < 300 ? Math.round(num/20.0) * 20 : 300;
def calorieExpendituresWeightCategory(float weight){
    int nearest = 0;        
    int min = Integer.MAX_VALUE
    CalorieExpenditures.properties.each {
        if(it.toString().startsWith("lbs")){
            int j = it.toString().substring(it.toString().indexOf("lbs"))               
            int k = (weight - j) * (weight - j)             
            if(k < min){
                min = k
                nearest = j
            }
        }
    }   
    return nearest          
}

This works for me i think simple and works for me.

Thanks for the answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top