2

I have an array of positive/negative ints

int[] numbers = new int[10];
numbers[0] = 100;
numbers[1] = -34200;
numbers[2] = 3040;
numbers[3] = 400433;
numbers[4] = 500;
numbers[5] = -100;
numbers[6] = -200;
numbers[7] = 532;
numbers[8] = 6584;
numbers[9] = -945;

and lets say i want to display a toast message when the program finds the closest value to myNum

int myNum = 600; 

Is this possible? and if so how can i implement this?

P.S no i do not want to use System.out.println() as i intend on using the IF statement for something else, i just thought displaying a toast message would be the simplest thing.

Ashraf Rahman
  • 467
  • 1
  • 5
  • 9

1 Answers1

3

How about:

public static void main(String[] args) {
    int[] numbers = new int[10];
    numbers[0] = 100;
    numbers[1] = -34200;
    numbers[2] = 3040;
    numbers[3] = 400433;
    numbers[4] = 500;
    numbers[5] = -100;
    numbers[6] = -200;
    numbers[7] = 532;
    numbers[8] = 6584;
    numbers[9] = -945;
    int myNum = 600;

    int distance = Math.abs(numbers[0]-myNum);
    int closest = numbers[0];

    for(int i=1;i<numbers.length;i++) {
        int itemDistance = Math.abs(numbers[i]-myNum);
        if(itemDistance < distance) {
            distance=itemDistance;
            closest=numbers[i];
        }
    }

    System.out.println("Closest number is:"+closest+" with distance: "+distance);
}

Which will print:

Closest number is:532 with distance: 68

Alternatively, you can do this with streams:

int result = Arrays.stream(numbers).reduce((i,j) -> Math.abs(i-myNum) < Math.abs(j-myNum) ? i : j ).getAsInt();

System.out.println(result);

Which will print 532 just like the previous one.

jrook
  • 3,459
  • 1
  • 16
  • 33
  • yes i have seen this answer as well and it led me to answer this question however this is using System.out.println(); and i would like to use a toast message using and IF statement – Ashraf Rahman Apr 17 '17 at 04:03
  • @AshrafRahman, is the problem just the result's type? Would it be acceptable if it gave a `Charsequence` instead of `int`? (https://developer.android.com/guide/topics/ui/notifiers/toasts.html) – jrook Apr 17 '17 at 04:07
  • @AshrafRahman if you want to use toast message then time to learn the Toast class – 0xDEADBEEF Apr 17 '17 at 04:18
  • @jrook no thats not my issue, my issue is with the IF statement, how can i create an if statement that will display the toast message WHEN the program has found a value closest to int myNum = 600;, yes this answer does find the closest value but how can i incorporate an IF statement to display the toast rather than System.out.println? – Ashraf Rahman Apr 17 '17 at 04:31
  • @AshrafRahman, in the bounds of this question, `when` is exactly the time the `System.out.println()` call is reached in the code. – jrook Apr 17 '17 at 05:01
  • You cannot possibly know which element in the array has the smallest distance from `myNum` unless you traverse the whole array at least once. So whatever you do, you will need to wait until the loop does its job ( or the stream returns the value). – jrook Apr 17 '17 at 05:02
  • Maybe you want to send a toast everytime a smaller distance is reached while traversing the array? – jrook Apr 17 '17 at 05:03