Home > database >  array search with exception in Java
array search with exception in Java

Time:05-05

Can you please help me with this. I need to find a value in array and if there is no such a value make an exception about it. i did wth if else in a loop it is print 4 time NOT FOUND. Thank you very much.

public class Search {
    public static int arraySearch(int [] array, int value){
        for(int i = 0; i < array.length; i  ){
            if(array[i] == value){
                System.out.println(value);
            }
        }
             
    }


    public static void main(String[] args) {
        int [] array = { 23, 6, 9, 10};
        arraySearch(array, 6);

    }
}

CodePudding user response:

This is one approach. The actual exception throwing can be built in.

public class Search {
    public static int arraySearch(int[] array, int value) {
        int result = -1;
        for (int i = 0; i < array.length && result < 0; i  ) {
            if (array[i] == value) {
                // Found! We want to know the index. We know the value
                result = i;
            }
        }
        return result;
    }


    public static void main(String[] args) {
        int[] array = { 23, 6, 9, 10 };
        System.out.println(arraySearch(array, 6));

    }
}

CodePudding user response:

As I pointed out in the comments, throwing an exception when searching for a missing element in an array is not really the best solution, but in another comment you say this is a requirement, so here it goes.

There are a couple of things we need to do

  1. A custom exception we're going to throw, because there's no exception in the standard library for "I have not found an element in an array", and for good reason;
  2. The arraySearch method needs to declare it throws the exception
  3. The arraySearch method should return a value, not print it
  4. If no value is returned (i.e. if we reached the end of the loop and have not returned yet) arraySearch should throw our custom exception
  5. main needs to catch the exception and act accordingly

Here's the code. Be sure to understand it, don't just copy and paste it.

public class Search {

    private static class ElementNotFoundException extends Throwable {}

    public static int arraySearch(int [] array, int value) throws ElementNotFoundException {
        for(int i = 0; i < array.length; i  ){
            if(array[i] == value){
                return value;
            }
        }

        throw new ElementNotFoundException();
    }


    public static void main(String[] args) {
        int [] array = { 23, 6, 9, 10};
        try {
            System.out.println(arraySearch(array, 60));
        } catch (ElementNotFoundException e) {
            System.out.println("Element not found");
        }
    }
}

Finally, I suspect you misunderstood your homework and you actually need to return the index of the element you found, not its value (which is not very useful). I'll leave that as an exercise for you to solve.

  • Related