We help IT Professionals succeed at work.

Matching a number to an element of an integer array.

Flex Tron
Flex Tron asked
on
Dear Gurus,
I am new to Java and trying to write a code where need to match a number 'y' from an array list x[].
If teh number matches, it should give the index value else return the value '-1'
I have written the code and it works...except that when the value doesn't match, it returns '-1' multiple times.
Is there a way that it returns '-1' only once.? Thankyou !

import java.io.*;
public class findLast {
    public static void main(String[] args) {
        try {
            boolean matchFound = false;
            int[] x = {7, 8, 9, 10, 11, 12};
            int y = 4;

            for (int i = x.length - 1; i >= 0 && matchFound == false; i--) {
                if (x[i] == y) {
                    System.out.println("Index is " + i);
                    matchFound = true;
                }
                else if (matchFound == false){
                    System.out.println(-1);
                }
            }

        }
        catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
        }
    }
}

Open in new window


Output:
C:\MS\src>java findLast
-1
-1
-1
-1
-1
-1
Comment
Watch Question

Flex TronDeveloper

Author

Commented:
I tried with another code but again the same issue. How to stop '-1' from getting printed again. ?

import java.io.*;
public class findLast {
    public static void main(String[] args) {
        try {
            int[] x = {7, 8, 9, 10, 11, 12};
            int y = 9;

            for (int i = x.length - 1; i >= 0; i--) {
                if (x[i] == y) {
                    System.out.println("Index is " + i);
                }
            }
            System.out.println(-1);
        }
        catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
        }
    }
}

Open in new window

Top Expert 2016

Commented:
How to stop '-1' from getting printed again. ?
Put in your loop (inside the if block)

break;

Open in new window

Flex TronDeveloper

Author

Commented:
Thanks for your inputs CEHJ. I changed the code a sbelow....but the output is still coming wrong.

Output:
C:\src>java findLast
I
ndex is 2
-1

The correct Output should be only '2' here

import java.io.*;
public class findLast {
    public static void main(String[] args) {
        try {
            int[] x = {7, 8, 9, 10, 11, 12};
            int y = 9;

            for (int i = x.length - 1; i >= 0; i--) {
                if (x[i] == y) {
                    System.out.println("Index is " + i);
                    break;
                }
            }
            System.out.println(-1);
        }
        catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
        }
    }
}

Open in new window

Yeah, because your loop breaks as it should and then goes straight on to print -1.
Flex TronDeveloper

Author

Commented:
@krakatoa
Thanks...But then how then stop -1 from printing.?

Commented:
Using  CEHJ's  break idea is good. But, your last code prints -1 irregardless of the value of matchFound.  You need to bring back conditional .
import java.io.*;
public class F {
    public static void main(String[] args) {
        try {
            boolean matchFound = false;
            int[] x = {7, 8, 9, 10, 11, 12};
            int y = 10;

            for (int i = x.length - 1; i >= 0 && matchFound == false; i--) {
                if (x[i] == y) {
                    System.out.println("Index is " + i);
                    matchFound = true;
					break;
                }
            }
            if (matchFound == false){
                System.out.println(-1);
            }
        }
        catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
        }
    }
}

Open in new window

 You really could use just the following.
public class F {
    public static void main(String[] args) {
            boolean matchFound = false;
            int[] x = {7, 8, 9, 10, 11, 12};
            int y = 10;
            for (int i = x.length - 1; i >= 0; i--) {
                if (x[i] == y) {
                    System.out.println("Index is " + i);
                    matchFound = true;
					break;
                }
            }
            if (matchFound == false){
                System.out.println(-1);
            }
    }
}

Open in new window

Flex TronDeveloper

Author

Commented:
Thanks...But I need to fit in the null point Exception in it.

catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
Actually all you need to do is to use "return" instead of "break", and you can leave the rest of the code as it is.
But what you ought to be doing is calling a method which returns an int - either the found value or -1. On the lines of :

import java.io.*;
public class findLast {
    public static void main(String[] args) {
        try {
            int[] x = {7, 8, 9, 10, 11, 12};
            int y = 9;

            /*for (int i = x.length - 1; i >= 0; i--) {
                if (x[i] == y) {
                    System.out.println("Index is " + i);
                    break;
                    //return;
                }
            }*/
            //System.out.println(-1);
            System.out.println("Index is " +find(y,x));
        }
        catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
        }
    }
    
    
    public static int find(int n, int[] arr){
        
        
        for(int i = arr.length - 1; i >= 0; i--){
            if(arr[i]==n){
                return i;
            }
            
        }
        return -1;
    }
    
    
}

Open in new window

Top Expert 2016

Commented:
But what you ought to be doing is calling a method which returns an int - either the found value or -1.
Quite right
Also NPE should never be caught - it's a programming error, not an exceptional event
Top Expert 2016

Commented:
Personally i prefer one exit point only from methods, so
    public static int find(int n, int[] arr) {
        int foundIndex = -1;

        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] == n) {
                foundIndex = i;
                break;
            }

        }
        return foundIndex;
    }

Open in new window

. . . prefer one exit point only


Yes; a subtle, and meticulous piece of concluding PC finesse there.
BTW Flex Tron, you don't need :

import java.io.*;
Top Expert 2016

Commented:
Also, class name in Java begin upper case and are in camel case

http://technojeeves.com/index.php/aliasjava1/106-java-style-conventions
It would also be slightly more "conventional" to use the common idiom in your loop, which is to iterate upwards when possible :

for(int i = 0; i < x.length; i++){

Open in new window


. . . avoiding the tautology of using -1 (as a token of the stop condition), along with the less-than-or-equal-to operator, <=, in your mildly counter-intuitive countdown direction.

(Course, if your array is sorted, which it appears to be, you could use a binary search algo., eventually).
Top Expert 2016

Commented:
The last index is required so it's more efficient to iterate backwards. To use the out-of-the-box binary search in Java, you'd have to reverse-sort first, then adjust the index, which would be messy. I suspect though that the fact that the array is sorted is accidental
The last index is required

Why's that?

It could be any of the ints in an array such as the one given initially, which is : {7, 8, 9, 10, 11, 12}

Where does the 'last index' come into the search when looking for the number '4' for instance ? None of the elements of the array are repeated afaics.
Top Expert 2016

Commented:
Why's that?
because the class is called findLast

btw, class names should be noun-based, not verb-based, so a better name would be
LastIndexFinder

Open in new window

because the class is called findLast

. . . which makes it a programming goal in name only, based on assumptions inferred by the reader.
In any case, if you use (CEHJ) your code, but without the break command and using an ascending loop, like this :

public static int find(int n, int[] arr) {
        int foundIndex = -1;

        for (int i = 0; i < arr.length;  i++) {
            if (arr[i] == n) {
                foundIndex = i;
                
            }

        }
        return foundIndex;
    }

Open in new window


it will return the required result.
Top Expert 2016

Commented:
That IS true, but you run a risk of unnecessary iterations, which is absent in reverse iteration (with break)
That’s true also, but the sought number could be anywhere if the series is unsorted. And if the series includes duplicates which somehow randomly end up being adjacent, then it wouldn’t make any time difference.
Top Expert 2016

Commented:
With indeterminate order, the most efficient way to get the last index is to iterate from the end, backwards (as indeed as Flex Tron is doing already)
NorieAnalyst Assistant

Commented:
How about not using a loop?
import java.util.Arrays;

public class findLast {
    public static void main(String[] args) {
        
            // array to search
            int[] arrint = {7, 8, 9, 10, 11, 12};

            Integer[] arrInt = new Integer[arrint.length];

            Arrays.setAll(arrInt, i -> arrint[i]);

            // value to find
            int val =10;

            int idx = Arrays.asList(arrInt).indexOf(new Integer(val));
        
            System.out.println(idx);

        }
    }

Open in new window

With indeterminate order, if the sought number was 12333, and was at index 0, and only appeared once, and the rest of array stretched from 1 to 12332,  then iterating from the end would be the slowest possible choice.
Top Expert 2016

Commented:
How about not using a loop?
That does use a loop - just not visibly

Presumably the code is a learning exercise so shortcuts using the API are probably not wanted, even if it's better to use them in real code
Presumably the code is a learning exercise so shortcuts using the API are probably not wanted

I was going to make the same comment. If you don't 'get' (hand-made) loops, then you don't go anywhere in coding.