Avatar of Flex Tron
Flex TronFlag 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
Avatar of Flex Tron
Flex Tron
Flag of United States of America image

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

break;

Open in new window

Avatar of Flex Tron
Flex Tron
Flag of United States of America image

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

Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Yeah, because your loop breaks as it should and then goes straight on to print -1.
Avatar of Flex Tron
Flex Tron
Flag of United States of America image

ASKER

@krakatoa
Thanks...But then how then stop -1 from printing.?
Avatar of rrz
rrz
Flag of United States of America image

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

Avatar of Flex Tron
Flex Tron
Flag of United States of America image

ASKER

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

catch (NullPointerException e) {
            System.out.print("NullPointerException Caught");
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

. . . prefer one exit point only


Yes; a subtle, and meticulous piece of concluding PC finesse there.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

BTW Flex Tron, you don't need :

import java.io.*;
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

http://technojeeves.com/index.php/aliasjava1/106-java-style-conventions
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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).
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

because the class is called findLast

. . . which makes it a programming goal in name only, based on assumptions inferred by the reader.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That IS true, but you run a risk of unnecessary iterations, which is absent in reverse iteration (with break)
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo