Avatar of Flex Tron
Flex Tron
Flag for United States of America asked on

Matching a number to an element of an integer array.

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
Java

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
Flex Tron

ASKER
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

CEHJ

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

break;

Open in new window

Flex Tron

ASKER
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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
krakatoa

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

ASKER
@krakatoa
Thanks...But then how then stop -1 from printing.?
rrz

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Flex Tron

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

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

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.
ASKER CERTIFIED SOLUTION
krakatoa

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CEHJ

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
CEHJ

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

krakatoa

. . . prefer one exit point only


Yes; a subtle, and meticulous piece of concluding PC finesse there.
krakatoa

BTW Flex Tron, you don't need :

import java.io.*;
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

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

http://technojeeves.com/index.php/aliasjava1/106-java-style-conventions
krakatoa

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).
CEHJ

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
krakatoa

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.
CEHJ

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

krakatoa

because the class is called findLast

. . . which makes it a programming goal in name only, based on assumptions inferred by the reader.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
krakatoa

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.
CEHJ

That IS true, but you run a risk of unnecessary iterations, which is absent in reverse iteration (with break)
krakatoa

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
CEHJ

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)
Norie

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

krakatoa

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

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
krakatoa

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.